from tkinter import *
root = Tk()
mybutton = Button(root, text="Click Me!)
mybutton.pack
root.mainloop
I am using this code but still I am not able to see the button in the window
This code wouldn't execute neither pack method nor the mainloop because you are missing the parentheses when invoking functions. When you invoke functions without parentheses, you are only referencing to them instead of calling, so it means that the code inside the function will never be executed.
from tkinter import *
root = Tk()
mybutton = Button(root, text="Click me!")
mybutton.pack()
root.mainloop()
Modifying your code as shown above should solve your issue.
Related
I have a script which has two tkinter.Tk() objects, two windows. One is hidden from the start (using .withdraw()), and each has a button which hides itself and shows the other (using .deiconify()). I use .mainloop() on the one shown in the beginning. Everything works, but when I close either window, the code after the mainloop() doesn't run, and the script doesn't end.
I suppose this is because one window is still open. If that is the case, how do I close it? Is it possible to have a check somewhere which closes a window if the other is closed?
If not, how do I fix this?
The essence of my code:
from tkinter import *
window1 = Tk()
window2 = Tk()
window2.withdraw()
def function1():
window1.withdraw()
window2.deiconify()
def function2():
window2.withdraw()
window1.deiconify()
button1 = Button(master=window1, text='1', command=function1)
button2 = Button(master=window2, text='2', command=function2)
button1.pack()
button2.pack()
window1.mainloop()
Compiling answers from comments:
Use Toplevel instead of multiple Tk()s. That's the recommended practice, because it decreases such problems and is a much better choice in a lot of situations.
Using a protocol handler, associate the closing of one window with the closing of both. One way to do this is the following code:
from _tkinter import TclError
def close_both():
for x in (window1,window2):
try:
x.destroy()
except TclError:
pass
for x in (window1,window2):
x.protocol("WM_DELETE_WINDOW", close_both)
I am new to tkinter and have been using:
from tkinter import *
but have read this is bad practice.
I rewrote a very small bit of code to start using the following:
import tkinter as tk
However when I run the rest of the code. I get the error:
label.place(relx=0.4, rely=0.35, anchor=CENTER)
NameError: name 'CENTER' is not defined
root = tk.Tk()
label = tk.Label(root, text="I am a label widget")
label.place(relx=0.4, rely=0.35, anchor=CENTER)
button = tk.Button(root, text="I am a button")
label.pack()
button.pack()
root.mainloop()
Is this a namespace issue? How can I solve the problem?
* gets all the sub packages. Using import tkinter as tk just changes the name of the package from tkinter to tk.
You have not told your script CENTER is part of tkinter. (you did this automatically when you used *) but now you must do by explicitly telling CENTER is part of tkinter:
tk.CENTER
CENTER is a variable(actually they usually are referred to as constants) of tkinter module which equals to 'center'. So simply replace the line with:
label.place(..., anchor='center')
I have a script in python, but now I am trying to have a GUI interface that has some text boxes that a user can fill in values to some variables, and then click 'run' to run the script.
What would the easiest way to approach this be?
Thanks a ton
To create GUI in python there are lots of packages available. I am using one of them (given below):
from Tkinter import *
master = Tk()
text_box = Entry(master)
text_box.pack()
text_box.focus_set()
def callback():
print text_box.get()
button = Button(master, text="ok", width=10, command=callback)
button.pack()
mainloop()
It will print input text given by user on terminal.
Consider the following code snippet:
from tkinter import *
import tkinter.filedialog as fd
def mycallback(event):
fname = fd.askopenfilename()
print(fname)
root = Tk()
b1 = Button(root, text='Hello!')
b1.bind('<Button-1>', mycallback)
b1.pack()
root.mainloop()
After pressing the button b1 an open-dialog appears as supposed. If I press OK or CANCEL after choosing a file, the program crashes with exit code 139.
What's the problem here?
I'm using Python 3.4 on OS X 10.6.8.
Calling a function when clicking a button can be done using the button's callback argument.
So instead of binding <Button-1> to the button you should use
b1 = Button(root, text='Hello!', command=mycallback)
You should then also remove the event argument from the mycallback function, since command doesn't pass any arguments.
How this solves your problem, I really dont know. But according to your comment it does.
For more information on the Button (and any other) widget, see effbot.org
Ok, I would like to put together a Python/Tkinter dialog box that displays a simple message and self-destructs after N seconds. Is there a simple way to do this?
You can use the after function to call a function after a delay elapsed and the destroy to close the window.
Here is an example
from Tkinter import Label, Tk
root = Tk()
prompt = 'hello'
label1 = Label(root, text=prompt, width=len(prompt))
label1.pack()
def close_after_2s():
root.destroy()
root.after(2000, close_after_2s)
root.mainloop()
Update: The after docstring says:
Call function once after given time.
MS specifies the time in milliseconds.
FUNC gives the function which shall be called.
Additional parameters are given as parameters to the function call.
Return identifier to cancel scheduling with after_cancel.
you could also use a thread.
this example uses a Timer to call destroy() after a specified amount of time.
import threading
import Tkinter
root = Tkinter.Tk()
Tkinter.Frame(root, width=250, height=100).pack()
Tkinter.Label(root, text='Hello').place(x=10, y=10)
threading.Timer(3.0, root.destroy).start()
root.mainloop()