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')
Related
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.
I have a Program that displays text on GUI Screen with Multiple Labels. But all the Labels are showing text in the new line and I want to show text in single line. Code is Below:
from tkinter import *
import tkinter as tk
win = Tk()
win.title("Label Screen")
win.geometry("800x600+50+50")
win.config(bg='white')
label1=Label(win, text="Label 1", font=("Calibri",24,"bold"), bg='white')
label1.pack(pady=15)
label2=Label(win, text="Label 2", font=("Calibri",24,"bold"), bg='white')
label2.pack(pady=15)
label3=Label(win, text="Label 3", font=("Calibri",24,"bold"), bg='white')
label3.pack(pady=15)
win.mainloop()
Output:
By default, the pack() method packs to the top, so you'll get a vertical stack of labels by default. You want to pack to the left to easily get all the labels on the same row. To do that, change:
labelX.pack(pady=15)
to
labelX.pack(side=tk.LEFT, pady=15)
Also, it's not ideal to import tkinter twice. Best to import it just once, as
import tkinter as tk
and then make sure to use tk. before all the methods, attributes, and classes.
Do this :
label1.pack(side=tk.LEFT,pady=15)
label2.pack(side=tk.LEFT,pady=15)
label3.pack(side=tk.LEFT,pady=15)
Further read : https://effbot.org/tkinterbook/pack.htm
so i have a simple problem but i have no idea what's wrong so have a look:
from tkinter import *
from time import sleep
root = Tk()
l1 = Label(root, text="Ok")
l1.pack()
sleep(5)
l2 = Label(root, text="Great")
l2.pack()
sleep(10)
l3 = Label(root, text="Nice")
l3.pack()
root.mainloop()
All i wanted to do is make a window, and then display those label's one after another so the window would pop up with just l1 and after 5s the l2 would also appear and then after another 5s l3 should show up. Instead i don't see this window at all for 10s and then it appear with all the label's at once. Can someone help me fix this ?
First of all, dont use from tkinter import *.
From this answer :
from Tkinter import * imports every exposed object in Tkinter into your current namespace. import Tkinter imports the "namespace" Tkinter in your namespace and import Tkinter as tk does the same, but "renames" it locally to 'tk' to save you typing
you want only one object of a module, you do from module import object or from module import object as whatiwantittocall
from module import * is discouraged, as you may accidentally shadow ("override") names, and may lose track which objects belong to wich module.
Now for your question, you need to use root.after(timeInMillisecond, functionToCall) and add the label in the function:
import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text="Quit", command=root.destroy)
button.pack()
l1 = tk.Label(root, text="Ok")
l1.pack()
def add_label():
l1 = tk.Label(root, text="Ok")
l1.pack()
root.after(500, add_label)
root.after(500, add_label)
root.mainloop()
PS : if someone with 1500+ reputation could add a tags "root.after" or something like this, it's the main point of the question
With the default theme on Linux, I can't get the button to have a border. Here's my attempt:
from tkinter import *
from tkinter.ttk import *
tk = Tk()
style = Style()
style.configure('TButton.border', background='red')
button = Button(tk, text='Hello')
button.pack()
mainloop()
I have found theming very difficult because it is not clear what I can change. Sometimes changes seem to do nothing.
It depends on what theme you're using; not all themes associate all widgets with all the elements they might have, and some (e.g., the OSX native theme, aqua) really very tightly control what the look and feel of many elements are (since the theme engine in that case delegates to the native drawing code). It's entirely possible that the theme you're using just doesn't permit red borders at all.
Try switching to a different themeā¦
ttk.Style().theme_use('clam')
Simple proposal ... if you really want a border. Understand it should not be satisfactory.
import Tkinter as tk
root = tk.Tk()
def nothing(event=None):
print "click"
bgbutton= tk.Button(root, )
bgbutton.pack()
bgbutton.configure(relief=tk.GROOVE, borderwidth=5, background="#2244FF", activebackground="#FF0000", highlightcolor="#00FF00")
button= tk.Button(bgbutton, text="Glance at my border", command=nothing)
button.pack()
button.configure(relief=tk.GROOVE, borderwidth=2)
root.mainloop()
I am working on a program that requires multiple windows, and the first one to appear is the login window, I used the Toplevel widget in order to make other windows its children, but this code keeps showing two windows instead of one.
from Tkinter import Frame, Toplevel
from ttk import Label, Entry, Button
class loginWindow(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.title("Title")
self.frame = Frame(self)
self.frame.pack()
self.__make_layout()
self.mainloop()
def __make_layout(self):
self.frame.user_name_label = Label(text="User name:")
self.frame.user_name_text = Entry()
self.frame.user_name_label.grid(row=0, column=0)
self.frame.user_name_text.grid(row=0, column=1)
self.frame.password_label = Label(text="Password:")
self.frame.password_text = Entry()
self.frame.password_label.grid(row=1, column=0)
self.frame.password_text.grid(row=1, column=1)
self.frame.login_button = Button(text="Login")# , command=self.__create_window)
self.frame.login_button.grid(row=2, column=0, columnspan=2)
if __name__ == '__main__':
win1 = loginWindow()
All of the widgets created in _make_layout are created without a parent. This means they're children of the default root. You need to pass a parent to each of them, the same way you do to the Frame. Like this:
self.frame.user_name_label = Label(self.frame, text="User name:")
self.frame.user_name_text = Entry(self.frame)
# etc.
When I run your exact code, I don't get a second window, on any platform I try. The closest I get is on OS X, where an entry for the default root window appears in the Window menu, but the window itself still doesn't appear and the widgets all end up on the Toplevel (although not on the Frame where you wanted them). But it certainly would be legal for Tkinter to show a second window here, and put some or all of your widgets on it.
This must be a platform dependent issue, since abarnert isn't having issues with multiple windows. I use OS X with XQuartz and the following code gives me two windows:
from Tkinter import Toplevel, Tk
Toplevel().mainloop()
However, this code gives me one window:
from Tkinter import Toplevel, Tk
Tk().mainloop()
I believe your first window should be declared Tk() and subsequent windows should be Toplevel().