I wanted make window which will reappear if closed but will only close if user presses a button. I tried so many times but cant make. Plz help.
from Tkinter import *
x='y'
while x!='break':
def something(x):
x='break'
root=tkinter.Tk()
button=tkinter.Button(root, text='Break', command=lambda:something(x))
button.pack()
root.mainloop()
print('done')
When you set x using x = "break", you set the local variable which means that the global variable x is still "y". So to solve your problem just make sure that you use the global variable for x. This is a simple example:
import tkinter as tk
def something():
# Use the global variable for `loop_running`
global loop_running
loop_running = False
# You might also want to add: root.destroy()
loop_running = True
while loop_running:
root = tk.Tk()
button = tk.Button(root, text="Break", command=something)
button.pack()
root.mainloop()
print("Done")
Related
I was trying to make a stopwatch in python but every time it stops working beacause of overflow, can someone please fix this??
Code:
import time
from tkinter import *
cur=time.time()
root = Tk()
def functio():
while True:
s = time.time()-cur
l1 = Label(root,text=s)
l1.pack()
l1.destroy()
time.sleep(0.5)
Button(root,text='Start',command=functio).pack()
root.mainloop()
The while loop will block tkinter mainloop from handling pending events, use after() instead.
Also it is better to create the label once outside the function and update it inside the function:
import time
# avoid using wildcard import
import tkinter as tk
cur = time.time()
root = tk.Tk()
def functio():
# update label text
l1['text'] = round(time.time()-cur, 4)
# use after() instead of while loop and time.sleep()
l1.after(500, functio)
tk.Button(root, text='Start', command=functio).pack()
# create the label first
l1 = tk.Label(root)
l1.pack()
root.mainloop()
Note that wildcard import is not recommended.
Flow of execution can never exit your endless while-loop. It will endlessly block the UI, since flow of execution can never return to tkinter's main loop.
You'll want to change your "functio" function to:
def functio():
s = time.time()-cur
l1 = Label(root,text=s)
l1.pack()
l1.destroy()
root.after(500, functio)
That being said, I'm not sure this function makes much sense: You create a widget, and then immediately destroy it?
You'll want to do something like this instead:
import time
from tkinter import *
root = Tk()
def functio():
global timerStarted
global cur
# check if we started the timer already and
# start it if we didn't
if not timerStarted:
cur = time.time()
timerStarted = True
s = round(time.time()-cur, 1)
# config text of the label
l1.config(text=s)
# use after() instead of sleep() like Paul already noted
root.after(500, functio)
timerStarted = False
Button(root, text='Start', command=functio).pack()
l1 = Label(root, text='0')
l1.pack()
root.mainloop()
I have a problem to detect/check button press in python tkinter !
I have a variable click i want that if my button is clicked then it becomes True
for ex:
this is my code:
buttonClicked=False
myButton=Button()
I want something like this:
if myButton is pressed:
buttonClicked=True
Thanks for your help!
I am not aware of any internal tkinter method to check if a button is pressed.
However you could connect the Button with a function that changes the value of a global variable, like in the following:
from Tkinter import *
master = Tk()
def callback():
global buttonClicked
buttonClicked = not buttonClicked
buttonClicked = False # Bfore first click
b = Button(master, text="Smth", command=callback)
b.pack()
mainloop()
The code, changes the variable value from False to True (or reverse) every time you press the button.
I think that you could make a function to change the value of buttonClicked, and, when the button is clicked, it executes that function (whose only purpose is to change the value of buttonClicked).
The complete code could go as follows:
from tkinter import *
buttonClicked = False
def changeValue():
if buttonClicked:
buttonClicked=False
if not buttonClicked:
buttonClicked=True
tk = Tk()
btn = Button(tk, text="Put whatever text you want here, to tell the person what pressing the button will do", command=changeValue())
btn.pack()
If this answer help, I would appreciate you tell me! :).
This is a changed/edited version, with a loop for logic that changes the value of buttonClicked. In the part of code that says "if not buttonClicked:" you could change to an "else:" statement. #
Try this:
from tkinter import *
value = 1
def change_value():
global value
value -= 1
if value == 0:
print("button pressed")
value = 1
else:
pass
tk = Tk()
btn = Button(tk, text="your_text", command=change_value)
btn.pack()
Just put the code you want to run inside a function like this:
def when_clicked():
#code you want here
button = Button(window,command=when_clicked)
(may be a bit late but oh well)
What I want to to do is to avoid opening the same window if it's already open.
So, I have a window and a button, and a button that opens another window, I want to click the button again and if the second window it's already open, change the focus to the second window or if it's not open, open it.
I tried with secondwindow.winfo_exists() but due that the function of the button that launches the second window is out the secondwindow function, it returns me that my secondwindow is not defined.
Way apart of this, I added a second button to check if the second window is created instead of checking calling the same function again.
Any way to do this? This is part of the code I have used:
def startwind1():
wind1 = tk.Tk()
wind1.title('Window 1')
w1button1 = ttk.Button(wind1,text='Launch Window 2',command=startwind2).pack()
w1button2 = ttk.Button(wind1,text='Check if Window 2 exists',command=checkwind2).pack()
wind1.mainloop()
def startwind2():
wind2 = tk.Toplevel()
wind2.title('Window 2')
def checkwind2():
if wind2.winfo_exists() == 1:
print('Window 2 exists')
else:
print('Window 2 not exists )
Hope you can help me!
Inside startwind2 you have to use global wind2 to assign window to global variable and then it will be available in other functions.
It also need to create wind2 = None so variable will be available even before you create window.
import tkinter as tk
from tkinter import ttk
wind2 = None
def startwind1():
#global wind2
#wind2 = None
wind1 = tk.Tk()
wind1.title('Window 1')
w1button1 = ttk.Button(wind1,text='Launch Window 2', command=startwind2)
w1button1.pack()
w1button2 = ttk.Button(wind1,text='Check if Window 2 exists',command=checkwind2)
w1button2.pack()
wind1.mainloop()
def startwind2():
global wind2
wind2 = tk.Toplevel()
wind2.title('Window 2')
def checkwind2():
if (wind2 is not None) and wind2.winfo_exists():
print('Window 2 exists')
else:
print('Window 2 not exists')
startwind1()
You can also do this with an if statement. It's a different approach but works well, and is very simple.
win1 = None
def startwind1():
if wind1 is not None and win1.winfo_exists():
pass
else:
global wind1
wind1 = tk.Tk()
wind1.title('Window 1')
wind1.mainloop()
Im trying to make a little program that endlessly prints out numbers inside GUI window, I can not find a way to print the out put of the function in a text box inside the GUI window instead of the python shell, please help, here is my code so far...
import sys
from tkinter import *
root = Tk()
def number(event):
x = 420
while True:
x +=420
print(x^70)
button_1 = Button(root, text="Start...")
button_1.bind("<Button-1>", number)
button_1.pack()
root.mainloop()
Thanks Harvey
You'll find it hard to constantly insert a value into a widget. The widget does not update with each insert. You can think of it has having a temporary variable for it. It can be accessed during the loop (as shown with print). However you'll notice that the widget itself doesn't update until the loop is over. So if you have while True then your widget will never update, and so you won't have the numbers streaming into the widget.
import sys
from tkinter import *
root = Tk()
def number():
x = 420
while x < 8400: # Limit for example
x +=420
textbox.insert(END, str(x^70)+'\n')
print(textbox.get(1.0, END)) # Print current contents
button_1 = Button(root, text="Start...", command=number) #Changed bind to command, bind is not really needed with a button
button_1.pack()
textbox = Text(root)
textbox.pack()
root.mainloop()
I have problem with my Tkinter code in Python 2.7. I have main window with one widget (button). I want to redraw window (and change value of variable -> add one widget) after click on the button. Where is problem? I think that problem can be, that every loop of mainloop change variable to 0. Thank you!
from Tkinter import *
def function():
global variable
variable = 0
main.update()
variable = 0
main = Tk() #New Tk window
if variable == 1:
Checkbutton(main, text="test").pack()
Button(main, text="Change", command=function).pack()
main.mainloop()
You never set variable to 1, and you should use functions (and classes) when working with GUIs.
from Tkinter import *
main = Tk() #New Tk window
variable = 0
def function():
global variable
variable = 1
newThing()
def newThing():
global variable
if variable==1:
Checkbutton(main, text="test").pack()
variable = 0
Button(main, text="Change", command=function).pack()
main.mainloop()