By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

While loop causes GUI to disappear

P: 2
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(GPIO.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_distance" pixel
canvas.create_rectangle(xlinedistance, 0, canvas_width, canvas_height *0.9, fill="black")
for x in rangpython probleme(xlinedistance,canvas_width,xlinedistance) :
a= x / xlinedistance - 1
canvas.create_line(x, 0, x, canvas_height, fill="#333333",dash=(4, 4))
canvas.create_text(x+3, canvas_height-10, fill="green", text=str(10*a) + 'Mhz', anchor="e")
# horizontal lines at an interval of "line_distance" pixel
for y in range(ylinedistance,canvas_height,ylinedistance):
b= y / ylinedistance - 1
canvas.create_line(0, y, canvas_width, y, fill="#333333",dash=(4, 4))
canvas.create_text(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_width, height=canvas_height)
grid.pack()
xlinedistance= canvas_width/11
ylinedistance= canvas_height/10
bezel(grid,xlinedistance, ylinedistance)
grid.create_line(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()
Jul 7 '17 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 619
The while() runs continuously and so hogs the cpu so nothing else has a chance to run. Use Tkinter's after() instead.
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. if sys.version_info[0] < 3:
  3.     import Tkinter as tk     ## Python 2.x
  4. else:
  5.     import tkinter as tk     ## Python 3.x
  6.  
  7.  
  8. class TestClass():
  9.  
  10.    def __init__(self):
  11.       """ A simple example of after() instead of a while()
  12.       """
  13.       self.top = tk.Tk()
  14.       self.top.title("Test of After")
  15.       self.top.geometry("200x150+10+10")
  16.  
  17.       self.ctr = 1
  18.       self.printit()
  19.       self.top.mainloop()
  20.  
  21.    def printit(self):
  22.       print "print 1", self.ctr
  23.       self.ctr += 1
  24.  
  25.       ## simulate if a condition is True
  26.       if self.ctr < 10:
  27.           ## pause 1/10 of a second to let other things
  28.           ## process and do again
  29.           self.top.after(100, self.printit)
  30.       else:
  31.           print "\nloop ended"
  32.  
  33. ##====================================================================
  34. if __name__ == '__main__':
  35.    CT=TestClass()  
Jul 9 '17 #2

P: 2
Thanks for the help. I'll try after().
Jul 11 '17 #3

P: 3
ooh cool useful info.. thanks for the posts
Jul 11 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.