473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(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
3 1756
dwblas
626 Expert 512MB
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
Thanks for the help. I'll try after().
Jul 11 '17 #3
Lilit1
3
ooh cool useful info.. thanks for the posts
Jul 11 '17 #4

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

Similar topics

1
by: Andreas Paasch | last post by:
Hi there, below a little code snippet that causes me some headaches. I had a similar one working fine, until I needed to redo my code on this one page entirely. Now my problem is, that the test...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
3
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
6
by: mgcclx | last post by:
For loop and while loop. which one is faster? I see many articles fighting over it and different people come up with different results.
1
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
5
by: jure87 | last post by:
hello everyone im new to this forum and need some help...i have a java code called flight.java and another code called checkflights.java, the purpose of this is to readin a text that is called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.