Trying a scenario where in I wanted to proceed in the for loop based on threading mechanism [for syncing/wait mechanism] as shown in the code below.
#!/tools/bin/python
# Global Import Variables
import Tkinter
from Tkinter import *
import subprocess
import shlex
import os
from PIL import Image, ImageTk
import time
import string
import threading
class test_template:
def __init__(self, master):
self.master = master
self.next_button = None
self.fruit_lbl = None
self.yes_button = None
self.no_button = None
self.yes_no_button_done = threading.Event()
# For Loop Goes From 0 To 10
for i in range (10):
if not (self.fruit_lbl):
self.fruit_lbl = Label(root, text="Do You Want To Change The Color Of Fruit %d"%i)
self.fruit_lbl.grid(row=1, column=0)
if not (self.yes_button):
self.yes_button = Button(root, background="lawn green", activebackground="forest green", text="YES", command=self.yes_button_code).grid(row=2, column=1)
if not (self.no_button):
self.no_button = Button(root, background="orange red", activebackground="orangered3", text="NO", command=self.no_button_code).grid(row=2, column=2)
while not self.yes_no_button_done.isSet():
self.yes_no_button_done.wait()
def yes_button_code(self):
print "Inside YES Button Config"
def no_button_code(self):
print "Inside No Button Config"
self.yes_no_button_done.set()
# Top Local Variables
root = Tk()
#root.configure(background='black')
# Top Level Default Codes
my_gui = test_template(root)
root.mainloop()
Its like the for loop needs to wait until I press either Yes Or No, until then the while loop should be waiting for the event set.
But somehow the threading/wait mechanism is not working fine i.e. as soon I launch the code, its goes into the while loop and goes haywire. Am I missing anything on the above code ? Share in your comments !!
If I have undertood you correctly I dont think that you need to use threading at all. Consider the following:
from Tkinter import *
class test_template:
def __init__(self, master):
self.master = master
self.loop_count = IntVar()
self.loop_count.set(1)
l = Label(self.master, text='Change colour of fruit?')
l.pack()
count = Label(self.master, textvariable = '%s' % self.loop_count)
count.pack()
yes = Button(self.master, text = 'YES', command = self.loop_counter)
yes.pack()
no = Button(self.master, text = 'NO', command = self.loop_counter)
no.pack()
def loop_counter(self):
if self.loop_count.get() == 10:
print 'Finished'
self.master.quit()
else:
self.loop_count.set(self.loop_count.get()+1)
print self.loop_count.get()
root = Tk()
GUI = test_template(root)
root.mainloop()
Let me know what you think.
Cheers,
Luke
Related
I have two widgets to work with, a text input, and a button, both are created inside a function. What I want to happen is the user types in their name and then clicks the button to submit the answer. What I want the computer to do, is on the button press it will read whats inside the text and the save it to a variable. Once it saves it, it will print it out.
The code below is bad because it runs through the if statement immediately without the consulting of the button press.
There has to be a simpler solution. Also this may not be PEP 8 or whatever please be patient because I'm new.
import tkinter as tk
from tkinter import Tk, Label, Button
import sys
import time
import random
import threading
from tkinter import *
window = tk.Tk()
window.geometry("300x300")
window.title("GUI")
def start_screen():
reset()
start = tk.Label(window, text="start of game")
start.place(x=110,y=20)
play = Button(window, text= "play", command = start_game)
play.place(x=110,y=50)
helper = Button(window, text="help", command = help_screen)
helper.place(x=110,y=70)
def stuff():
global t
t = True
print(t)
return t
def text_handling():
global t
t = False
reset()#clears the screen
label = Label(window, text='')
question1= "what is your name?"
label.pack()
print_slow(label, question1, 40)#prints out letters slowly
#here is the part I'm having problems with
name = Entry(window)
name.pack()
but = Button(window, text="enter", command= stuff)
but.pack()
print(t)
if t == True:
myPlayer.name = name.get()
print(myPlayer.name)
def start_game():
reset()
bt = tk.Button(window,text="Enter", bg="orange", command =
text_handling)
bt.place(x=100,y=100)
start_screen()
I would need a bit of help with my code I'm writing... and although when I start the code then comes directly such a window which asks me if I want to close it.
Does anyone have an idea where this could come from?
The code should open a window where I have some buttons to open some other windows with sliders to control DMX Lights.
Here is the code:
import tkinter as tk
from tkinter import ttk
from tkinter import *
from print_dict import pd
from time import sleep
from tkinter import messagebox
Pultdatas = {'DMXtype':512,
'columns':2,
'rows':8,
'slider':8,
'modes':4
}
root = Tk()
root.configure(background='gray')
root.title('DMX-Pult v2')
Screens = {'mainscreen':[root,{}]}
def on_closing(screen):
if messagebox.askokcancel("Quit", f"Do you want to quit Scanner {screen}?"):
Screens[screen][0].destroy()
def setmode(screen,mode):
print(f'setmode on screen {screen} to {mode}')
for i in Screens[screen][1]['modesel']:
print(i)
i.config(bg='gray')
Screens[screen][1]['modesel'][mode].config(bg='green')
def pressed(btn):
print('pressed Button: ' + str(btn))
if not Screens.__contains__(btn):
Screens[btn] = []
Screens[btn].append(Tk())
Screens[btn][0].configure(background='gray')
Screens[btn][0].title(f'Scanner {btn}')
Screens[btn].append({})
Screens[btn][1]['modesel'] = []
for i in range(Pultdatas['modes']):
Screens[btn][1]['modesel'].append(Button(Screens[btn][0], text=f"mode {i+1}", bg='gray', fg='white', command=lambda name = i:setmode(btn,name)))
Screens[btn][1]['modesel'][i].grid(row=i,column=0)
setmode(btn,0)
else:
if Screens[btn][0].winfo_exists() == 1:
sleep(0.2)
print('This window exist.')
Screens[btn][0].attributes('-topmost', True)
Screens[btn][0].update()
Screens[btn][0].attributes('-topmost', False)
else:
Screens[btn] = []
Screens[btn].append(Tk())
Screens[btn][0].configure(background='gray')
Screens[btn][0].title(f'Scanner {btn}')
Screens[btn].append({})
Screens[btn][1]['modesel'] = []
for i in range(Pultdatas['modes']):
Screens[btn][1]['modesel'].append(Button(Screens[btn][0], text=f"mode {i + 1}", bg='gray', fg='white',
command=lambda name=i: setmode(btn, name)))
Screens[btn][1]['modesel'][i].grid(row=i, column=0)
Screens[btn][0].protocol("WM_DELETE_WINDOW", lambda name = btn:on_closing(name))
setmode(btn, 0)
print()
def close():
if messagebox.askokcancel("Quit", f"Do you want to quit?"):
for screen in Screens:
print(f'closed {Screens[screen][0].title()} Succesfully')
Screens[screen][0].destroy()
tmpint = 0
Screens['mainscreen'][1]['Back'] = Button(root, text='close all',bg='gray',fg='white',command=close)
Screens['mainscreen'][1]['Back'].grid(row=0,column=0)
for column in range(Pultdatas['columns']):
for row in range(Pultdatas['rows']):
tmpint += 1
Screens['mainscreen'][1][tmpint] = Button(root, text=f"Scanner {tmpint}", bg='gray', fg='white', command=lambda name = tmpint:pressed(name))
Screens['mainscreen'][1][tmpint].grid(row=row+1,column=column)
pd(Screens)
root.protocol("WM_DELETE_WINDOW", close())
root.mainloop()
You can just change this line of code
root.protocol("WM_DELETE_WINDOW", close())
To this:
root.protocol("WM_DELETE_WINDOW", close)
You don't need to write (), we can just write handler not as function.
For more info you can visit here
I have a large code where a button press is supposed to run a code that will take roughly 15 seconds to complete. Within this time I want to display a label that says "Processing, please wait" or something of that sort. However in python, the whole GUI created using tkinter will freeze and unfreeze once the procedure is over. How do I get around to doing this? I created a smaller code so that I can explain easier.
from tkinter import *
from threading import Thread
import os
import sys
import time
master = Tk()
master.geometry("500x500")
master.resizable(False,False)
def tryout():
sign2.config(text = "AAA")
for x in range(5):
print(x)
time.sleep(1)
sign2.config(text = "BBB")
for x in range(5):
print(x)
time.sleep(1)
sign2.config(text = "CCC")
def close_window():
master.destroy()
sys.exit()
sign1 = Label(master, text = "VNA GUI").grid(pady=10, padx=10)
sign2 = Label(master, text = "Choose option to continue")
sign2.grid(pady=10, padx=10, ipadx=50)
Button(master, text='Exit', command=close_window).grid(pady=10, padx=20)
butTest = Button(master, text='test', command=tryout)
butTest.grid(pady=10, padx=20)
master.mainloop( )
So in this code I expect to see 'AAA' on the label first, followed by 'BBB' at the middle of the count from 0 to 4, and then 'CCC' at the end of the final count from 0 to 4. What happens here is the GUI freezes at the beginning, the count carries on and I just see 'CCC'. How do I get around this?
There are only a few changes necessary to do that with threading.
First create a function start_tryout:
def start_tryout():
Thread(target=tryout, daemon=True).start() # deamon=True is important so that you can close the program correctly
Then create the button with the new command:
butTest = Button(master, text='test', command=start_tryout)
Then it should no longer freeze the gui and you should be able to see the label change.
You can try threading. I've made changes below to the code and tested it here, and it worked.
from tkinter import *
from threading import Thread
import os
import sys
import time
import threading # NEW
master = Tk()
master.geometry("500x500")
master.resizable(False,False)
def tryout():
sign2.config(text = "AAA")
for x in range(5):
print(x)
time.sleep(1)
sign2.config(text = "BBB")
for x in range(5):
print(x)
time.sleep(1)
sign2.config(text = "CCC")
def close_window():
master.destroy()
sys.exit()
def thread(): # NEW
threading.Thread(target=tryout).start() # NEW
sign1 = Label(master, text = "VNA GUI").grid(pady=10, padx=10)
sign2 = Label(master, text = "Choose option to continue")
sign2.grid(pady=10, padx=10, ipadx=50)
Button(master, text='Exit', command=close_window).grid(pady=10, padx=20)
butTest = Button(master, text='test', command=thread) # Changed
butTest.grid(pady=10, padx=20)
master.mainloop( )
I'm coding a timer app, which has encountered a lot of difficulties, but one of the first ones has been in making a loop that can be broken by the press of a button. I've looked into it, and my research shows I should use threading, but I couldn't figure out how it would work.
What I decided to try, then, was to make an exception when I invoked a keyboard interrupt, and then make a button that calls that same interrupt. However, my current code refuses to interrupt when I ctrl-c.
My sample code looks like this
from Tkinter import *
from sys import exit
class Timer:
def __init__(self, master):
buttonstart = Button(master, text = "Start", fg = "blue", command = self.start)
buttonstart.grid(row = 1, column = 0)
buttonquit = Button(master, text = "Quit", fg = "blue", command= quit)
buttonquit.grid(row = 1, column = 2)
global timertext
timertext = DoubleVar()
timertext.set(0)
display = Label(master, textvariable = timertext)
display.grid(row = 0, column = 0)
timertext.set(timertext)
def timerlogic(self):
pass
def pause(self):
pass
def start(self):
global timertext
try:
while True:
#do things
except KeyboardInterrupt:
print "Interrupted"
def lap(self):
pass
root = Tk()
app = Timer(root)
root.mainloop()
root.destroy()
Basically, I don't think my code as it stands is viable, but I don't know how to edit it to make a loop I can interrupt as needed.
You set some variable to True or False. Also, a while loop interrupts the Tkinter loop so Tkinter will do nothing until the while loop exits. Use Tkinter's after function instead.
from Tkinter import *
##from sys import exit
class Timer:
def __init__(self, master):
self.master=master
buttonstart = Button(master, text = "Start", fg = "blue", command = self.start)
buttonstart.grid(row = 1, column = 0)
buttonquit = Button(master, text = "Quit", fg = "blue", command=self.quitit)
buttonquit.grid(row = 1, column = 2)
self.timertext = DoubleVar()
self.timertext.set(0)
display = Label(master, textvariable = self.timertext)
display.grid(row = 0, column = 0)
## timertext.set(timertext) ## Huh!!
self.timeit=False
def increment_timer(self):
ctr=int(self.timertext.get())
self.timertext.set(ctr+1)
if self.timeit:
self.master.after(500, self.increment_timer)
def start(self):
self.timeit=True
self.increment_timer()
def quitit(self):
self.timeit=False
root = Tk()
app = Timer(root)
root.mainloop()
I'm trying to use messagebox.showinfo to show a message when my counter reach 4.
import tkinter
from tkinter import *
global cptBalle
global cptPrise
cptBalle = 0
cptPrise = 0
cptRetrait = 0
cptManche = 1
ptsVisiteur = 0
ptsReceveur = 0
coupSurVisiteur = 0
coupSurReceveur = 0
erreurVisiteur = 0
erreurReceveur = 0
equipeAuBaton = "visiteur"
def balle():
global cptBalle
global cptPrise
cptBalle += 1
if cptBalle == 4:
messagebox.showinfo(title="Balle", message="Testtest")
cptBalle = 0
cptPrise = 0
def prise():
pass
def fausse_balle():
pass
def retrait():
pass
def balle_passee():
pass
def mauvais_lancer():
pass
def sacrifice():
pass
def simple():
pass
def double():
pass
def triple():
pass
def circuit():
pass
def atteint():
pass
def erreur():
pass
def creer_bouton():
btnBalle = Button(app, text="Balle", command=balle)
btnBalle.grid(row=1, column=0)
btnPrise = Button(app, text="Prise", command=prise)
btnPrise.grid(row=2, column=0)
btnFausse_balle = Button(app, text="Fausse balle", command=fausse_balle)
btnFausse_balle.grid(row=3, column=0)
btnRetrait = Button(app, text="Retrait", command=retrait)
btnRetrait.grid(row=4, column=0)
btnBalle_passee = Button(app, text="Balle passee", command=balle_passee)
btnBalle_passee.grid(row=5, column=0)
btnMauvais_lancer = Button(app, text="Mauvais lancer", command=mauvais_lancer)
btnMauvais_lancer.grid(row=6, column=0)
btnSacrifice = Button(app, text="Sacrifice", command=sacrifice)
btnSacrifice.grid(row=7, column=0)
btnSimple = Button(app, text="Simple", command=simple)
btnSimple.grid(row=8, column=0)
btnDouble = Button(app, text="Double", command=double)
btnDouble.grid(row=9, column=0)
btnTriple = Button(app, text="Triple", command=triple)
btnTriple.grid(row=10, column=0)
btnCircuit = Button(app, text="Circuit", command=circuit)
btnCircuit.grid(row=11, column=0)
btnAtteint = Button(app, text="Atteint", command=atteint)
btnAtteint.grid(row=12, column=0)
btnErreur = Button(app, text="Erreur", command=erreur)
btnErreur.grid(row=13, column=0)
root = tkinter.Tk()
root.title("Baseball!")
root.geometry("750x350")
app = Frame(root)
app.grid()
creer_bouton()
root.mainloop()
The first button "btnBalle" call the function "Balle()".
It works when I run it in Python IDLE but when I use PyCharm, it doesn't.
Obviously that's not your whole program, but… let's assume your real program is calling Test() multiple times, but isn't actually starting the Tk runloop anywhere, or even creating a top-level Tk object. So, any Tkinter windows you try to display will never show up. (Actually, on some platforms, like OS X, it will show up—but it shouldn't, and if it doesn't on your platform, that's not a bug.)
Why does it work in IDLE? Because IDLE itself is written in Tkinter, and the tricks it uses to allow you to use the interactive interpreter while you've got a GUI program running also allow you to get away with this. That's not something you should rely on.
I ran into the same problem when attempting to use the messagebox class.
The basis of the code was:
import tkinter as tk
tk.messagebox.showinfo( --- )
I resolved the issue by adding:
from tkinter import messagebox
and then calling:
messagebox.showinfo( --- )
Hope this helps.