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.
Related
I would like to eliminate any trace of "pack" from the code and use only "place", in order to position the scrollbar near the listbox and stretch it along the entire height of the listbox. Previously it worked, then the code was modified with pack.
Now I want to remove all packs and use only place
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
window = tk.Tk()
window.geometry("800x800")
frame_title = tk.Frame(window)
frame_title.pack(fill='both', expand=True, pady=5, padx=5)
listbox_title = tk.Listbox(frame_title, selectbackground="#960000", selectforeground="white", bg="white", width = 50, height = 20)
listbox_title.place(x=1, y=1)
listbox_title.configure(font=("Arial", 12))
scrollbar_title = tk.Scrollbar(frame_title)
scrollbar_title.place(x=500, y=1)
scrollbar_title['command'] = listbox_title.yview
listbox_title.config(yscrollcommand=scrollbar_title.set)
listbox_title.bind('<Double-Button-1>')
text_download = ScrolledText(window, bg="white", width = 50, height = 10)
text_download.place(x=1, y=500)
text_download.configure(font=("Arial", 14))
window.mainloop()
You could bind the scroll bar to the listbox or text widget directly then it will dynamically change when you change the size of the parent widget as well.
scroll_bar = tk.Scrollbar(frame_title)
scroll_bar.config(command=listbox_title.yview)
listbox_title.config(yscrollcommand=scroll_bar.set)
## For Verticle Scrollbar ##
scroll_bar.place(in_=listbox_title, relx=1.0, relheight=1.0, bordermode="outside")
## For Horzontal Scrollbar ##
scroll_bar.place(in_=listbox_title, relx=0.0, rely=1.0, relwidth=1.0, bordermode="outside")
You can then place the listbox as normal.
Update code:
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
window = tk.Tk()
window.geometry("800x800")
frame_title = tk.Frame(window)
frame_title.pack(fill='both', expand=True, pady=5, padx=5)
listbox_title = tk.Listbox(frame_title, selectbackground="#960000", selectforeground="white", bg="white", width = 50, height = 20)
listbox_title.place(x=1, y=1)
listbox_title.configure(font=("Arial", 12))
## Vertical Scroll Bar ##
scrollbar_title = tk.Scrollbar(frame_title, command=listbox_title.yview, orient="vertical")
listbox_title.config(yscrollcommand=scrollbar_title.set)
scrollbar_title.place(in_=listbox_title, relx=1.0, relheight=1.0, bordermode="outside")
listbox_title.bind('<Double-Button-1>')
text_download = ScrolledText(window, bg="white", width = 50, height = 10)
text_download.place(x=1, y=500)
text_download.configure(font=("Arial", 14))
window.mainloop()
I have an Python3 Tkinter Programm. I have 3 Frames in the Main Window and in one Frame an canvas with scroll Option - now i want resitze the Canvas Area .
Now if i resize it moves the Scroll Bar for Y out the Window and the scrollbar for x works also not as expected (get bigger but slide area don't change)
How i Mange it to resize an Canvas in an grid Layout - The Window must be the same size , the Scrollbas must be updatet and the Canvas Plane must be bigger.
an excerpt from my code:
import tkinter as tk
def menu_build():
caninfo[0] += 10
cangui.configure(width = caninfo[0])
#cangui.configure(scrollregion=cangui.bbox("all"))
def gui():
master = tk.Tk()
master.title( "Easy Switch" )
master.geometry("480x320")
frametop = tk.Frame(master, bg="blue", bd=2)
frametop.grid(column=0,row=0)
frameex = tk.Frame(master, bg="yellow", bd=2)
frameex.grid(column=1,row=1)
framegui = tk.Frame(master, bg="red", bd=2)
framegui.grid(column=0, columnspan=2, row=1)
menu = tk.Menu(master)
master.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="Config", menu=filemenu)
filemenu.add_command(label="Resize",command=menu_build)
global cangui
cangui = tk.Canvas(framegui, width=385, height=250)
#caninfo = [385,250]
cangui.grid(row=1, column=2)
scroll_x = tk.Scrollbar(framegui, orient="horizontal", command=cangui.xview)
scroll_x.grid(row=2, column=2, sticky="ew")
scroll_y = tk.Scrollbar(framegui, orient="vertical", command=cangui.yview)
scroll_y.grid(row=1, column=3, sticky="ns")
cangui.configure(yscrollcommand=scroll_y.set,xscrollcommand=scroll_x.set)
cangui.configure(scrollregion=cangui.bbox("all"))
cwcb = tk.Checkbutton(framegui, text="ccw").grid(row=2,column=0)
cangui.create_arc(90,90,110,110,style=tk.PIESLICE,width=4,start=0,extent=300)
master.mainloop()
global caninfo
caninfo = [385,250]
if __name__ == "__main__":
gui()
no need to resize the canvas Area
wrote an extra funktion
win = [int(cangui.cget("width")),int(cangui.cget("height"))]
draw_xy = cangui.bbox("all")
swin = (min(0,draw_xy[0]),min(0,draw_xy[1]),max(draw_xy[2],win[0]),max(draw_xy[3],win[1]))
cangui.configure(scrollregion=swin)
reason: canvas.bbox("all") gives only the positon from most upper/left grafic and i want 0/0
scrollbar in Top level window created for a canvas is not working and how do I get region of canvas. where canvas size varies with number of buttons in it
I have created frame in tk(root) and one widget in a frame creates Top level window , which has multiple Buttons in a Frame.Number of Button varies with list. That Frame(which has buttons in it) exists in a canvas. My problem is that after putting scroll widget canvas do not moves
from tkinter import *
root = Tk()
root.geometry("500x200")
my_app= Frame(root)
my_app.pack()
my_window = Toplevel(my_app, bg='brown')
my_window.geometry("500x200+300+500")
top_window = Frame(my_window, bd=2, relief=SUNKEN)
top_window.grid_columnconfigure(0, weight=1)
yscrollbar = Scrollbar(top_window)
yscrollbar.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(top_window, bd=0, yscrollcommand=yscrollbar.set)
canvas.config(scrollregion=(0, 0, 500, 1000))
canvas.grid(row=0, column=0, sticky=N+S+E+W)
yscrollbar.config( command = canvas.yview)
top_window.pack()
my_f = Frame(canvas)
def ins_ind(m):
print(m)
results =
["one","two","three","four","five","six","seven","eight","nine","ten"]
ins_list=[]
for ind, result in enumerate(results):
ins=str(result)
ins_list.append(ind)
ins_list[ind] = Button(my_f, text = ins, font='Times 12 bold',
bg='sandy brown',anchor=E, fg="black", command = lambda m=ins:
ins_ind(m) )
ins_list[ind].pack()
my_f.pack()
root.mainloop()
Scroll bar do no moves
For the buttons to move when you scroll the canvas you must put the buttons on the canvas.
Your code puts the buttons on the frame my_f. To put the buttons on the canvas you should use: canvas.create_window(x, y, window=button).
I put widgets on a window with .grid and noticed that the widgets have space around them. I fixed that on canvas widget with highlightthickness but this doesn't work with button widgets. It doesn't look cool when you have grey window background and there's white space around buttons.
P.S. I use python3 on macos Sierra
root = Tk()
root.configure(bg='grey')
w = Canvas(root, width=150, height=150, highlightthickness=0)
w.grid(row=1, column=1, sticky="nsew")
clear_btn = Button(text="Clear", width=15, command=lambda: w.delete("all"))
clear_btn.grid(row=2, column=1)
root.mainloop()
P.S. TKinter leaving borders around widgets that's what it looks like. The guy was advised to use highlightbackground, but when I screenshot widgets, I still have that blank space around the widget.
To get the button to fill the entire space of the grid box it occupies, try setting the sticky argument of the grid() function to "NSEW":
from tkinter import *
root = Tk()
root.configure(bg = "grey")
w = Canvas(root, width = 150, height = 150, highlightthickness = 0)
w.grid(row = 1, column = 1, sticky = "NSEW")
clear_btn = Button(text = "Clear", width = 15, command = lambda: w.delete("all"))
clear_btn.grid(row = 2, column = 1, sticky = "NSEW")
root.mainloop()
Before:
After:
Note: The appearance of these windows may vary slightly between operating systems and even between different visual themes on the same operating system.
I don't have OSX, so I cannot check, but I think that if you use ttk and a style different from the default one (like 'clam'), you might be able to configure things like you want.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.configure(bg='grey')
style = ttk.Style(root)
style.theme_use('clam')
w = tk.Canvas(root, width=150, height=150, highlightthickness=0)
w.grid(row=1, column=1, sticky="nsew")
clear_btn = ttk.Button(text="Clear", width=15, command=lambda: w.delete("all"))
clear_btn.grid(row=2, column=1)
root.mainloop()
I would like to display
a window
with a single Frame
and a Label in the Frame which would stretch to the whole width of the window
The following code
import Tkinter as tk
root = tk.Tk()
root.geometry("100x100")
# first column of root will stretch
root.columnconfigure(0, weight=1)
# a frame in root
upper_frame = tk.Frame(root)
# first column of upper_frame will stretch
upper_frame.columnconfigure(0, weight=1)
upper_frame.grid(row=0, column=0)
# a label in upper_frame, which should stretch
mylabel = tk.Label(upper_frame)
mylabel.grid(row=0, column=0)
mylabel.configure(text="hello", background="blue")
root.mainloop()
displays
Why isn't the Label stretching to the whole width of the window but is just as wide as the text?
Specifying sticky option when you call grid (e = east, w = west). Otherwise the widget in the cell is center-aligned.
upper_frame.grid(row=0, column=0, sticky='ew')
..
mylabel.grid(row=0, column=0, sticky='ew')