This question already has an answer here:
How to stop Tkinter Frame from shrinking to fit its contents?
(1 answer)
Closed 4 years ago.
I have two frames in the root. And I want to add a button in one of the frames. Both frames have different background colours. When I try to add a button in any of them, the frame that contains the button disappears.
Without button
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
root.mainloop()
Which results in...
With button
But after adding the button, with the following code...
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text="Load", fg="red", bg="black")
b1.pack(side=LEFT)
root.mainloop()
I get...
Now the button is visible but the frame and background colours are gone. What am I doing wrong?
Thanks for your help!
Your frame is actually disappearing it is just resize and that is why you cant see it. Add Rf.pack_propagate(False) to your frame it will prevent the frame from resizing when a new widget is added.
from tkinter import *
root= Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf=Frame(root,width=100, height=800, bg="black")
Rf.pack_propagate(False)
Rf.pack(side=RIGHT)
Lf=Frame(root,width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text= "Load", fg= "red", bg="black")
b1.pack(side=LEFT)
root.mainloop()
Related
I have 2 frames, one of them has a button "click". On clicking that button, the same button should be destroyed from the original frame and move to frame2. How to achieve it using tkinter.
there is tow frames frame1 and frame2 first we want to show this button using pack(). now we want to add function on command of my_btn then we want to destroy that button and re define it in the second frame.
final code:
from tkinter import *
window = Tk()
frame1 = Frame(window, width=50, height=50, bg='red')
frame2 = Frame(window, width=50, height=50, bg='blue')
frame1.propagate(0); frame2.propagate(0)
frame1.pack(); frame2.pack()
def click():
global my_btn
my_btn.destroy()
my_btn = Button(master=frame2)
my_btn.pack()
my_btn = Button(master=frame1, command=click)
my_btn.pack()
window.mainloop()
Hope this works.
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.
i want to fix the postion of scrollbar of scrolledtext widget of tkinter.
i'am creating a chatbot where after every new message there is need to drag the scrollbar down to see conversation which has a bad impact.
here is the code of scrolledtext
self.conversation = ScrolledText.ScrolledText(self,
state='disabled',borderwidth=5,
highlightthickness=1,
bg='#15202b',fg='#16202A',
font=('Arial Bold',8))
self.conversation.grid(column=0, row=1, columnspan=2, sticky='nesw', padx=3, pady=3)
from tkinter import *
from tkinter import scrolledtext
root = Tk()
scroll_x = Scrollbar(root, orient="horizontal")
text = scrolledtext.ScrolledText(root, wrap=NONE)
text.config(xscrollcommand=scroll_x.set)
scroll_x.configure(command=text.xview)
text.pack(fill=X)
scroll_x.pack(fill=X)
for i in range(10000):
text.insert(END, str(i)+"\n")
text.see("end")
root.update()
root.mainloop()
The command you are looking for is see("end")
text.see("end")
root.update()
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 have created a spinbox on frame1 and going to frame2 by clicking NEXT, From frame2 going back to frame 1 by clicking BACK button. By the time spinbox widget getting disappear. what could be the reason?
from Tkinter import *
def swap_frame(frame):
frame.tkraise()
root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken",cursor="arrow",background="#BCC3B9",highlightcolor="black")
root.resizable(width=False, height=False)
frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)
#item 1 spinbox
Platform = Spinbox(values=("SX-16F", "SX-12VP", "SX-16VP", "VSRM-A", "NRNT-A", "FX-8", "DX-48V"), width="32")
Platform.place(x=500, y=200, relheight=0.05)
Button1=Button(frame1, text="Next", width =10, height= 2, bg= "#dbd8d7", command=lambda:swap_frame(frame2))
Button1.place(x=580, y=580)
Button3=Button(frame2, text="Back", width =10, height= 2, bg= "#dbd8d7", command=lambda:swap_frame(frame1))
Button3.place(x=580, y=580)
frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)
root.mainloop()
Pass the master to your Spinbox widget. It currently defaults to Tk window which is only initially lifted, hence it gets blocked by both frames when either one is lifted. Replace:
Platform = Spinbox(values=(...), ...)
with:
Platform = Spinbox(frame1, values=(...), ...)