I need to center 3 labels vertically within the window. The labels are centered on-top of each other, but they are fixed at the top of the window.
What do I need to do to have them sit right in the middle of the window, (vertically and horizontally)?
Here is my code:
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("Question 2")
root.configure(background="green")
Label(root, text = "RED", fg="red", bg="black").pack()
Label(root, text = "WHITE", fg="white", bg="black").pack()
Label(root, text = "BLUE", fg="blue", bg="black").pack()
root.mainloop()
I think that in this case you can simply use a Frame widget as the parent of the labels and then pack the frame by setting the expand option to True:
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("Question 2")
root.configure(background="green")
parent = Frame(root)
Label(parent, text = "RED", fg="red", bg="black").pack(fill="x")
Label(parent, text = "WHITE", fg="white", bg="black").pack(fill="x")
Label(parent, text = "BLUE", fg="blue", bg="black").pack(fill="x")
parent.pack(expand=1) # same as expand=True
root.mainloop()
Related
First, I read the related discussion:
How can I put 2 buttons next to each other?.
However, I still confused about it. My codes:
#Import the required Libraries
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win, width= 40)
entry.pack()
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT)
win.mainloop()
Now I got
I want two buttons in the middle of screen just below the Entry (white box).
How to fix it? Thanks!
You can make another tk.Frame which is arranged horizontally and pack it below; for example:
entry= Entry(win, width= 40)
entry.pack()
buttons = ttk.Frame(win)
buttons.pack(pady = 5)
button1= ttk.Button(buttons, text= "Print", command=display_num)
button1.pack(side = LEFT)
button2= ttk.Button(buttons, text= "Clear", command= clear)
button2.pack()
Alternatively you can use the grid layout manager.
entry= Entry(win, width= 40)
entry.grid(row = 0, column = 0, columnspan = 2)
button1= ttk.Button(win, text= "Print", command=display_num)
button1.grid(row = 1, column = 0)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.grid(row = 1, column = 1)
Working with pack means working with parcels, therefore Imagine a rectangle around your widgets while we discuss further more. By default your values look like this:
widget.pack(side='top',expand=False,fill=None,anchor='center')
To get your widget in the spot you like you will need to define it by these parameters. the side determinates in which direction it should be added to. Expand tells your parcel to consume extra space in the master. fill tells your widget to strech out in its parcel in x or y or both direction. you can also choose to anchor your widget in your parcel in east,west,north,south or a combination of it.
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win)
entry.pack(fill='x')
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT,expand=1,anchor='ne')
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT,expand=1,anchor='nw')
win.mainloop()
To learn more about orginizing widgets and the geometry management of tkinter, see my answer here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my current code. I am looking to link the buttons I made to open a new window. I want the old Window to then close. When the new Window opens, how do I go about creating a new interface for that Window.
Code:
import tkinter as tk
HEIGHT = 950
WIDTH = 650
root=tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#80c1ff')
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
button = tk.Button(frame, text="Credit Score Checker")
font = ("Helvetica",20,"bold")
button.place(relx=-0, relheight=1, relwidth=0.27)
button = tk.Button(frame, text="Financial Advisor")
font = ("Helvetica",20,"bold")
button.place(relx=0.25, relheight=1, relwidth=0.27)
button = tk.Button(frame, text="Insurance Planner")
font = ("Helvetica",20,"bold")
button.place(relx=0.5, relheight=1, relwidth=0.27)
button = tk.Button(frame, text="Goal Setter")
font = ("Helvetica",20,"bold")
button.place(relx=0.75, relheight=1, relwidth=0.26)
lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
label = tk.Label(lower_frame, text="Summary of finances", bg='grey')
label.place(relx=0.5, rely=0, anchor='n', relwidth=0.9, relheight=1)
root.mainloop()
Here is an example of a button that opens up a new window when clicked I hope this helps
from tkinter import *
main = Tk()
main.geometry("1000x1000")
main.title("Welcome")
canvas = Canvas(main, width = 1500, height = 1000, bg = "spring green")
canvas.place(relx = 0.5, rely = 0.5, anchor = CENTER)
frame = Frame(main)
frame = Frame(main, width = 500, height = 500, bg = "cyan")
frame.place(relx = 0.5, rely = 0.5, anchor = CENTER)
frame3 = Frame(main, width=300, height=300, bg="orange")
frame3.place(relx = 0.5, rely = 0.5, anchor = CENTER)
# LABEL:
lbl = Label(main, text = "Hi", fg = "deep pink", font = ("Fixedsys",30), bg = "spring green")
lbl.place(relx = 0.5, rely = 0.5, anchor = CENTER)
# FUNCTION FOR BUTTON:
def on_click():
root = Tk()
frame2 = Frame(root)
frame2 = Frame(root, width = 1000, height = 1000, bg = "cyan")
frame2.place(relx = 0.5, rely = 0.5, anchor = CENTER)
root.geometry("700x700")
root.title("SUP")
label_new = Label(root, text = "There you go :)", font = ("Fixedsys", 22), fg = "red", bg = "black")
label_new.place(relx = 0.5, rely = 0.5, anchor = CENTER)
# BUTTON:
btn = Button(main, text = "this is a button", fg = "blue", font = ("Fixedsys",20), bg = "dark turquoise", command=on_click)
btn.place(relx = 1.0, rely = 0.0, anchor = NE)
main.mainloop()
If you really want to create a new window every time the button is pressed and display the new widgets on this window, then the other answers provide an excellent solution. But rather than create a new window to display the new widgets, I would recommend simply destroying the old widgets on the current window and putting up the new ones on the same window. It has the same effect as "closing the old window and creating a new one".
To destroy all the widgets on the screen, simply write a function like this and call it whenever you want to remove all the old widgets and put up new ones.
def clear() :
for widget in root.winfo_children() :
widget.destroy()
The above function iterates through all the widgets that have been put up on the window and destroys them, one by one.
Here is an example code to illustrate how this works.
import tkinter as tk
root = tk.Tk()
root.config(bg="#424242")
def clear() :
for widget in root.winfo_children() :
widget.destroy()
def window1() :
clear()
lbl1 = tk.Label(root,text="Hey there! Im part of window1!!",fg="white",bg="#424242")
lbl1.pack()
lbl2 = tk.Label(root,text="Im also a part of window1!!",fg="white",bg="#424242")
lbl2.pack()
btn1 = tk.Button(root,text="Press me to display window2",command=window2,fg="white",bg="#424242")
btn1.pack()
def window2() :
clear()
lbl1 = tk.Label(root,text="Hey there! Im part of window2!!",fg="white",bg="#424242")
lbl1.pack()
lbl2 = tk.Label(root,text="Im also a part of window2!!",fg="white",bg="#424242")
lbl2.pack()
btn1 = tk.Button(root,text="Press me to display window1",command=window1,fg="white",bg="#424242")
btn1.pack()
window1()
root.mainloop()
Here, as Cool Cloud has already said, a command parameter is passed to the button with the next function to be called. Both the functions,window1 and window2 call the clear function first which effectively clears the screen ,and then the new widgets are put up on the window.
So I created a frame in which I want to put two widgets with them being centered on the x-axis. When it's one object pack() centers it automatically. But I can't figure out two widgets. I tried with grid() but there is free space left on the right which makes it look unsymetrical as seen in the image.
how could I get what is seen on the right? (I'd prefer it being dont with pack() but if there is a solution with grid() and/or place() as well i'd appreciate those as well!)
here's the code for the left picture
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can use frame.pack() to easily position the frame in the top, middle of its parent.
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.pack()
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can put the label and button in another frame, and use pack() on that frame:
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
frame2 = Frame(frame)
frame2.pack() # default side='top'
label = Label(frame2, bg="lime", text="label", font=font.Font(size=20))
label.pack(side='left', fill='both')
button = Button(frame2, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.pack(side='left')
root.mainloop()
Working with Tkinter, I need to center entities. When trying to center labels, it will only center it within the first row, and not the window.
I want it centered within the entire window. i.e. the middle. So far, it is only the middle of the top. is this possible?
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
root = Tk()
# New window, but text appears in the center of the center (the absolute center).
def whatsup():
popup = Tk()
popup.title("Cadillac")
frame = Frame(popup)
frame.pack()
label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
label.pack()
root.title("I Love You")
# 1, 1
button = Button(root, text="Ayo girl", command=whatsup)
button.pack(side=LEFT)
# 1, 2, but to be 2, 2 soon after addition of new items.
canvas = Canvas(root, height=250, width=200)
imageOfCatherine=ImageTk.PhotoImage(Image.open('ccr_on_moon.jpg'))
canvas.create_image(-160, -100, anchor=NW, image=imageOfCatherine)
canvas.pack()
root.mainloop()
You can use grid instead of pack, with rowconfigure and columnconfigure methods like this :
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
root = Tk()
root.title("I Love You")
# New window, but text appears in the center of the center (the absolute center).
def whatsup():
popup = Tk()
popup.title("Cadillac")
frame = Frame(popup)
frame.pack()
label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
label.pack()
# 1, 1
button = Button(root, text="Ayo girl", command=whatsup)
button.grid(row=0, column=0, sticky='w')
# 1, 2, but to be 2, 2 soon after addition of new items.
canvas = Canvas(root, height=250, width=200)
imageOfCatherine=ImageTk.PhotoImage(Image.open('ccr_on_moon.jpg'))
canvas.create_image(-160, -100, anchor=NW, image=imageOfCatherine)
canvas.grid(row=1, column=1)
root.rowconfigure([0,1,2], weight=1)
root.columnconfigure([0,1,2], weight=1)
root.mainloop()
Answer to comment
This also works, but it's not centered the same way :
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
root = Tk()
root.title("I Love You")
# New window, but text appears in the center of the center (the absolute center).
def whatsup():
popup = Tk()
popup.title("Cadillac")
frame = Frame(popup)
frame.pack()
label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
label.pack()
# 1, 1
button = Button(root, text="Ayo girl", command=whatsup)
button.grid(row=0, column=0, sticky='w')
# 1, 2, but to be 2, 2 soon after addition of new items.
canvas = Canvas(root, height=250, width=200)
imageOfCatherine=ImageTk.PhotoImage(Image.open('ccr_on_moon.jpg'))
canvas.create_image(-160, -100, anchor=NW, image=imageOfCatherine)
canvas.grid(row=0, column=1, sticky='')
root.rowconfigure(0, weight=1)
root.columnconfigure([0,1], weight=1)
root.mainloop()
After some tinkering (no pun intended), I added expand=YES to frame.pack() in the whatsup() function.
def whatsup():
popup = Tk()
popup.title("Cadillac")
frame = Frame(popup)
frame.pack(expand=YES) # This was the changed line!
label = ttk.Label(frame, text="Wanna ride in my Cadillac?")
label.pack()
This allows for the popup text to become centered.
how do I position my label which says "Question One" in my def new_window() function. As you run it the label is being positioned at the bottom, And i want it to be applied on the top.
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=150, height=40)
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display.pack()
message.pack()
display2 = Label(root, width=100, height=30, bg='green')
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label1 = Label(text="A Quiz Game", font = ("Arial", "24"), padx=40,
pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
display2.pack()
button1.pack()
message_label1.pack()
message_label2.pack()
root.mainloop() # Runs the main window loop
You are packing in the wrong order. Do not pack display before your message. So just swapping the order will fix the issue.
Here is the code. Replace your def new_window(): with this
def new_window():
newWindow = Toplevel()
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display = Label(newWindow, width=150, height=40)
message.pack()
display.pack()
pack method just blindly packs the widget into the window. And the next pack will be done below it if there is space. So take care of the order while packing widgets :)