I am totally new to using tkinter and am playing around with grid currently. I have set my window to be 1000x500 and have an image at the top left like this...
window = Tk()
window.geometry("1000x500") #Width x Height
logo = PhotoImage(file="logo.gif")
Label (window, image=logo, bg="#f0f0f0") .grid(row=0, column=0)
T = Text(window, height=2, width=30)
T.insert(END, "Just a text Widget\nin two lines\n")
T.grid(row=2, column=0)
I would like the text widget to be placed at the bottom of the window, I have tried setting the row to something larger but it doesn't have any effect.
Where am I going wrong?
Apparently,The easiest way is to use .pack() instead of .grid().But if you really want to use .grid().You need to set the rowconfigure() to set the weight of row,And sticky="s" or sticky=S to make it in the bottom.
Your code can be:
from tkinter import *
window = Tk()
# window.geometry("1000x500") #Width x Height
logo = PhotoImage(file="xxx")
Label (window, image=logo, bg="#f0f0f0") .grid(row=0, column=0)
T = Text(window, height=2, width=30)
T.insert(END, "Just a text Widget\nin two lines\n")
T.grid(row=1, column=0, sticky=S)
window.grid_rowconfigure(1,weight=1)
window.mainloop()
Remember,if you want to always make it in the bottom and only use .gird(),you need to set the row weight of the text.So I suggest you put all the widget(except the Text widget in the bottom) in a Frame.And use .grid() in the Frame.The Frame and the Text use .pack().
Related
import tkinter as tk
win = tk.Tk()
win.title('使用者登入')
win.geometry('300x200')
lb1 = tk.Label(win,text='使用者帳號資料',font=('微軟正黑體',16),fg='yellow',bg='black')
lb1.pack(fill='x')
lb2 = tk.Label(win,text='帳號 : ABCDEF',height=4,width=26,font=('標楷體',14),bg='lightblue')
lb2.pack(side='left',anchor='nw',fill='x')
lb3 = tk.Label(win,text='注意',height=50,width=3,font=('微軟正黑體',12),bg='pink')
lb3.pack(anchor='se',fill='y',expand=True)
lb4 = tk.Label(win,text='密碼 : 123456',height=4,width=15,font=('標楷體',14),bg='lightgreen')
lb4.pack(anchor='sw',side='left')
win.mainloop()
The variable lb4 could not be shown on the tkinter window even though I have used pack() method. I would like to put the lb4 widget below lb2 and I had tried using anchor='sw' still not showing so any possible methods to have that widget be shown properly?
The pack method works properly, check that:
import tkinter as tk
win = tk.Tk()
win.title('使用者登入')
win.geometry('300x200')
# lb1 = tk.Label(win,text='使用者帳號資料',font=('微軟正黑體',16),fg='yellow',bg='black')
# lb1.pack(fill='x')
#
# lb2 = tk.Label(win,text='帳號 : ABCDEF',height=4,width=26,font=('標楷體',14),bg='lightblue')
# lb2.pack(side='left',anchor='nw',fill='x')
#
# lb3 = tk.Label(win,text='注意',height=50,width=3,font=('微軟正黑體',12),bg='pink')
# lb3.pack(anchor='se',fill='y',expand=True)
lb4 = tk.Label(win,text='密碼 : 123456',height=4,width=15,font=('標楷體',14),bg='lightgreen')
lb4.pack(anchor='sw',side='left')
win.mainloop()
If it does show anything is because the pack method add widget to the right of the previous on if it's anchored to "S"(outh), even if the new widget it's anchored to 'w' and sided to 'left'.
Since lb3 is anchor at 'se', any other widget is displayed outside the windows.
You shoud use .grid to properly design your window:
import tkinter as tk
win = tk.Tk()
win.title('使用者登入')
lb1 = tk.Label(win, text='使用者帳號資料', font=('微軟正黑體', 16), fg='yellow',
bg='black')
lb1.grid(row=0, column=0, columnspan=3, sticky='WE')
lb2 = tk.Label(win, text='帳號 : ABCDEF', height=4, width=26, font=('標楷體', 14),
bg='lightblue')
lb2.grid(row=1, column=0, sticky='WE')
lb3 = tk.Label(win, text='注意', width=3, font=('微軟正黑體', 12), bg='pink')
lb3.grid(row=1, column=2, rowspan=2, sticky='NS')
#
lb4 = tk.Label(win, text='密碼 : 123456', height=4, width=15, font=('標楷體', 14),
bg='lightgreen')
lb4.grid(row=2, column=0, sticky='w')
win.mainloop()
It looks like this:
Here, I removed the height of lb3 since it's not required (sticky 'NS' allow us to extend the widget to top and down of the rowspan).
You can change column, row, columnspan, rowspan as you want to create the layout you desire.
Each time you use pack, it allocates an entire side for the widget. Thus, the order in which you call pack matters. For example, once you put widget along the top, you can no longer put something to the right.
This is usually much easier to visualize when you group all of your pack statements together for all children in the same parent widget. I also recommend always explicitly defining the side parameter so that your intentions are crystal clear.
This is the proper order to use pack to get lb4 below lb2, with lb1 along the top and lb3 along the right side:
lb1.pack(side="top", fill='x')
lb3.pack(side="right", fill='y')
lb2.pack(side="top", fill='x')
lb4.pack(side="top", fill='x')
I tried running your code and using lb4 before lb3 fixes the problem, but it won't display below lb2 as you have already used side='left' in lb2 so it acquires the whole left side, to pack it below lb2 remove the side='left' in lb2 and put it in lb4.
It doesn't display lb4 at first because it packs it outside of the TKinter window .
Instead of using .pack() use .grid() to better place the labels in the window.
This works:
import tkinter as tk
win = tk.Tk()
win.title('使用者登入')
win.geometry('300x200')
lb1 = tk.Label(win,text='使用者帳號資料',font=('微軟正黑體',16),fg='yellow',bg='black')
lb1.pack(fill='x')
lb2 = tk.Label(win,text='帳號 : ABCDEF',height=4,width=26,font=('標楷體',14),bg='lightblue')
lb2.pack(anchor='nw',fill='x')
lb4 = tk.Label(win,text='密碼 : 123456',height=4,width=15,font=('標楷體',14),bg='lightgreen')
lb4.pack(side='left',anchor='sw')
lb3 = tk.Label(win,text='注意',height=50,width=3,font=('微軟正黑體',12),bg='pink')
lb3.pack(anchor='se',fill='y',expand=True)
win.mainloop()
I'm trying to center text in the frame using LabelFrame function. My code is below.
import tkinter as tk
window = tk.Tk()
for i in range(9):
for j in range(9):
frame = tk.LabelFrame(
master=window,
relief=tk.RAISED,
borderwidth=5,
width=50,
height=50,
text=i+j,
labelanchor = 'n'
)
frame.grid(row=i, column=j)
window.geometry("500x500")
window.mainloop()
Argument labelanchor that specify position gives only options on the edge of the frame. Is there any simple way to center text inside of the frame using LabelFrame?
Is there any simple way to center text inside of the frame using LabelFrame?
The text of the labelframe can only appear along the edges of the frame. If you wish for text to appear inside the frame, you must create a Label and add it to the frame.
Using a frame and a label
If you wish to put other widgets in the frame and don't want this text to interfere with the other widgets, this is a perfect opportunity to use place.
The following example adds the label "Hello, world" to appear in the center of the widget. A button is placed in the frame just to show that its placement is not affected by the label, or vise versa. The screenshots show what the frame looks like naturally and when the window is resized.
import tkinter as tk
root = tk.Tk()
lf = tk.Frame(root, bd=2, relief="groove")
lf_label = tk.Label(lf, text="Hello, world")
lf_label.place(relx=.5, rely=.5, anchor="c")
lf.pack(padx=20, pady=20, fill="both", expand=True)
b = tk.Button(lf, text="Click me")
b.pack(padx=10, pady=10)
root.mainloop()
Using only a label
You can add widgets inside any other widget. So, instead of a frame you could just use a label. By default, the text will be centered, and just like with the previous example it won't affect the layout of other widgets.
I haven't included screenshots because the results are identical to the previous example.
import tkinter as tk
root = tk.Tk()
lf = tk.Label(text="Hello, world", bd=2, relief="groove")
lf.pack(padx=20, pady=20, fill="both", expand=True)
b = tk.Button(lf, text="Click me")
b.grid(row=0, column=0)
root.mainloop()
Example
(mimics the relevant parts of the layout in my real code)
import Tkinter as tk
import ttk
# set up root
root = tk.Tk()
root.minsize(300, 50)
frame = ttk.Frame(root)
frame.grid(row=0, column=0, sticky=tk.EW)
# set up buttons that insert a short or a long string
textvar = tk.StringVar(value='foo')
def insert_short():
textvar.set('foo')
def insert_long():
textvar.set('foo'*30)
button_short = ttk.Button(frame, text='short', command=insert_short)
button_short.grid(row=0, column=0)
button_long = ttk.Button(frame, text='long', command=insert_long)
button_short.grid(row=0, column=0)
button_long.grid(row=0, column=1)
# set up label
# border for label to see its size
style = ttk.Style()
style.configure(
'Bordered.TLabel', foreground='black', borderwidth=1, relief='solid')
# make label extend to the right
frame.columnconfigure(2, weight=1)
# place label
label = ttk.Label(frame, textvariable=textvar, style='Bordered.TLabel')
label.grid(row=0, column=2, sticky=tk.EW)
# place some other widget under label to mimic my real code
ttk.Button(frame, text='some other widget').grid(row=1, column=2)
# TRIED, NOT WORKING:
#root.resizable(False, False)
#frame.propagate(False)
#frame.grid_propagate(False)
#label.propagate(False)
#label.grid_propagate(False)
root.mainloop()
Output
Question
How do I prevent label from extending the main window?
(Bonus question, but not important: is there a way to make the label scrollable if it gets too long?)
Attempts
I tried the following commands:
root.resizable(False, False)
frame.propagate(False)
frame.grid_propagate(False)
label.propagate(False)
label.grid_propagate(False)
You can create a scrollable label using an Entry in a read-only state and by using scrolling it will prevent the widget from extending the main window.
Try replacing your label definition with the following code:
child_frm = ttk.Frame(frame)
label = ttk.Entry(child_frm, textvariable=textvar, style='Bordered.TLabel', state='readonly')
scroll = ttk.Scrollbar(child_frm, orient='horizontal', command=label.xview)
label.config(xscrollcommand=scroll.set)
label.grid(row=0, sticky=tk.EW)
scroll.grid(row=1, sticky=tk.EW)
child_frm.grid(row=0, column=2)
By default, the width of a Label is calculated based on its contents. You can override this behavior by specifying a value for width when creating the Label.
label = ttk.Label(frame, textvariable=textvar, style='Bordered.TLabel', width=1)
Much to my surprise, when I update your code with this, the label doesn't shrink to a size suitable for displaying exactly one character. It appears that the sticky=tk.EW argument of your grid call ensures that the label stays as wide as the widest element in the column.
I am using following code and trying to center the text on labels:
from tkinter import *
root = Tk()
llist = ["first:","second label:","a really long label:","fourth:","fifth:"]
nrow = 0
for i in range(len(llist)):
# Label(root, text=llist[i], justify='right').grid(row=nrow, column=0) # does not work
# Label(root, text=llist[i], justify=RIGHT).grid(row=nrow, column=0) # does not work
# Label(root, text=llist[i], anchor=E).grid(row=nrow, column=0) # does not work
# Label(root, text=llist[i], anchor=E, justify=RIGHT).grid(row=nrow, column=0) # does not work
Label(root, text=llist[i]).grid(row=nrow, column=0, sticky=E) # WORKS;
Entry(root).grid(row=nrow, column=1)
nrow += 1
root.mainloop()
The text remains in center with options that I mention as not working in above code:
Only with sticky option in the grid() it works properly:
Why justify and anchor options of Label do not work in above code, even though they are mentioned at several places such as http://effbot.org/tkinterbook/label.htm ? How can justify and anchor be used to align text in Labels?
This is because you haven't set width for Label, if you don't set it, Label will fit its content. So in short, the text inside Label is worked with anchor option, but Label's length is the same as text's length. And what located at center is not text but Label.
If you manually set a width for it:
Label(root, text=llist[i], anchor="e", width=20).grid(row=nrow, column=0)
It will work, but it is difficult to manually specify the length of Label, so use grid's sticky option is much better.
I am trying to use grid() function to align the labels and option menu side by side. Here's the code which I used to create a simple GUI:
from Tkinter import *
win1 = Tk()
win1.title("Chumma")
#Option selection frame:
f3 = Frame(win1)
f3.grid(column=0,row=0)
f3.pack()
l1 = Label(f3, text="Select the function which you want to perform: ", bg = "yellow")
moduleList = StringVar(f3)
moduleList.set("Normal Walk") #to display the default module name
o1 = OptionMenu(f3, moduleList, "Normal Walk", "Brisk Walk", "Running", "Custom")
b3 = Button(f3, text="Execute the option", fg="blue")
b4 = Button(f3, text="Stop", fg="red")
#Packing the stuffs in required order:
l1.grid(row=0, column=0, sticky=W) #E means east
l1.grid_rowconfigure(0, weight=1)
l1.grid_columnconfigure(0, weight=1)
l1.pack(fill = X, padx = 5)
o1.grid(row=0,column=1)
o1.grid_rowconfigure(0, weight=1)
o1.grid_columnconfigure(1, weight=1)
o1.pack()
b4.pack()
win1.mainloop()
The result is:
I am expecting the option menu o1 to be at the right of the l1.
If I comment the pack command [ l1.pack() and o1.pack() ], the program is not displaying any GUI at all.
After you call grid, a couple of lines later you call pack which cancels out the use of grid. Use one or the other but not both for each widget. Sinc pack defaults to side='top', your widgets appear stacked on top of each other.
The reason you see nothing if you comment out those two calls to pack is because you are still calling b4.pack(), and you can't use both pack and grid for different widgets with the same parent.
Also, the calls to rowconfigure and columnconfigure need to be on the parent widget. Calling them on the label widget will only affect widgets you put inside the label (which is possible, but unusual)
I believe Tkinter does not allow mixing up packing schemes (grid, pack, place) in one frame. Here is example how to organize three widgets.
from Tkinter import *
root = Tk()
label = Label(root, text='blablabla')
someotherwidget = Entry(root)
button = Button(root, command=lambda: None, text='Boom')
label.grid(row=0, column=0)
someotherwidget.grid(row=0, column=1)
button.grid(row=1, column=0, columnspan=2)
root.mainloop()
Option 'columspan' is like how many columns you want to join to place the widget. We have 2 columns here so if we want to see button not below label but below of both label and someotherwidget we have to specify 'columnspan' option (obviously, analog for rows is 'rowspan')