While loop causes GUI to disappear

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frenchpark43
    New Member
    • Jul 2017
    • 2

    While loop causes GUI to disappear

    I am trying to make a replacemnt display for an old tube based instrument using a raspi and LCD. I have a start on the GUI code and it works but when I add the while loop at the end waiting for input the GUI does not display. when I remove the while loop it does display. Any help will be appreciated.

    Code below

    from Tkinter import *
    import numpy, math, serial
    import RPi.GPIO as GPIO
    import time
    GPIO.setmode(GP IO.BOARD)
    GPIO.setup(21, GPIO.IN,pull_up _down=GPIO.PUD_ UP) # USE PIN 21 INPUT, 25 GND
    def bezel(canvas, xlinedistance, ylinedistance): # Create the canvas
    # vertical lines at an interval of "line_dista nce" pixel
    canvas.create_r ectangle(xlined istance, 0, canvas_width, canvas_height *0.9, fill="black")
    for x in rangpython probleme(xlined istance,canvas_ width,xlinedist ance):
    a= x / xlinedistance - 1
    canvas.create_l ine(x, 0, x, canvas_height, fill="#333333", dash=(4, 4))
    canvas.create_t ext(x+3, canvas_height-10, fill="green", text=str(10*a) + 'Mhz', anchor="e")
    # horizontal lines at an interval of "line_dista nce" pixel
    for y in range(ylinedist ance,canvas_hei ght,ylinedistan ce):
    b= y / ylinedistance - 1
    canvas.create_l ine(0, y, canvas_width, y, fill="#333333", dash=(4, 4))
    canvas.create_t ext(5, y-10, fill="green", text=str(-10*b) + "db", anchor="w")


    master = Tk()
    master.wm_title ("HPSA")
    canvas_width = 640
    canvas_height = 480
    grid = Canvas(master, width=canvas_wi dth, height=canvas_h eight)
    grid.pack()
    xlinedistance= canvas_width/11
    ylinedistance= canvas_height/10
    bezel(grid,xlin edistance, ylinedistance)
    grid.create_lin e(xlinedistance , canvas_height/2, canvas_width, canvas_height/2, fill="red")
    while True:
    print("Test ")
    while (GPIO.input(21) ==TRUE):
    pass
    print("Starting Scan ")


    master.mainloop ()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    The while() runs continuously and so hogs the cpu so nothing else has a chance to run. Use Tkinter's after() instead.
    Code:
    import sys
    if sys.version_info[0] < 3:
        import Tkinter as tk     ## Python 2.x
    else:
        import tkinter as tk     ## Python 3.x
    
    
    class TestClass():
    
       def __init__(self):
          """ A simple example of after() instead of a while()
          """
          self.top = tk.Tk()
          self.top.title("Test of After")
          self.top.geometry("200x150+10+10")
    
          self.ctr = 1
          self.printit()
          self.top.mainloop()
    
       def printit(self):
          print "print 1", self.ctr
          self.ctr += 1
    
          ## simulate if a condition is True
          if self.ctr < 10:
              ## pause 1/10 of a second to let other things
              ## process and do again
              self.top.after(100, self.printit)
          else:
              print "\nloop ended"
    
    ##====================================================================
    if __name__ == '__main__':
       CT=TestClass()

    Comment

    • frenchpark43
      New Member
      • Jul 2017
      • 2

      #3
      Thanks for the help. I'll try after().

      Comment

      • Lilit1
        New Member
        • Jul 2017
        • 3

        #4
        ooh cool useful info.. thanks for the posts

        Comment

        Working...