why isn't my frame stretch completely in tkinter - python

I tried to stretch the frame using sticky='nsew' but it's not working properly. I have attached the screenshot here.
the white color window is my frame and i want to stick to all corners. i dont wanna mess up with the buttons that i made. its working if i set row=0 but in that case i lost the buttons.. i wanna keep that button too
from tkinter import *
root=Tk()
root.title("manage your cashflow")
#for setting full screen window
screen_width=root.winfo_screenwidth()
screen_height=root.winfo_screenheight()
print(screen_height, screen_width)
root.geometry("%dx%d" % (screen_width,screen_height))
root.configure(bg='grey')
#configure
Grid.rowconfigure(root,0,weight=1)
Grid.columnconfigure(root,0,weight=1)
Grid.columnconfigure(root,1,weight=1)
# creating tabs
up_button1= Button(root, text="the list of month", bg='#ebca87')
up_button1.grid(row=0,column=0, sticky='NEW')
up_button2=Button(root, text="the new month",bg='#ebca87')
up_button2.grid(row=0,column=1,sticky='NEW',pady=0)
#class for frames
class frame:
#method for
def frame2(self):
#for frame sticky
Grid.rowconfigure(root,1,weight=1)
Grid.columnconfigure(root,0,weight=1)
Grid.columnconfigure(root,1,weight=1)
#frame for listbox
frame2=Frame(root)
frame2.grid(row=1,column=0,columnspan=2,sticky="NSEW")
Grid.rowconfigure(frame2,0,weight=1)
Grid.columnconfigure(frame2,0,weight=1)
Grid.columnconfigure(frame2,1,weight=1)
salary_text=Label(frame2,text="salary")
salary_text.pack()
salary_entry=Entry(frame2)
salary_entry.pack()
frame_ob=frame()
frame_ob.frame2()
root.mainloop()
I want to stretch the frame to the buttons. how do I do it?
you guys can see in the screenshot that the white frame doesn't stick to all corners of the 2nd row and 1st and 2 column

Your main issue is Grid.rowconfigure(root,0,weight=1). This is causing the row where the buttons sit to expand at the same rate as the row the frame is in.
Just get rid of that line and it should fix your problem.
That said there are several things I would change with your code.
You use Grid.rowconfigure() and so on and this is not the standard usage. Is there any reason you are doing it this way? Normally I would expect to see frame2.rowconfigure(0, weight=1).
You build your frame as a class but don't actually do anything in it that would require it to be a class. You also do not inherit tk.Frame in your class. I would write it a little different here.
import * is frowned on. It can cause problems when maintaining code over time as it grows and makes it harder to know where the methods are from so trouble shooting can be harder. try import tkinter as tk and use the tk. prefix.
Reasons you might need or want to set a variable name for a widget is if you plan on interacting with it later. Like updating a label or if you want to set a variable name to make it easier to ID what that widget is for. Then you can do that but personally I prefer to avoid setting variables if they will never be modified later.
Your code can be reduced quite a bit.
Here is a non OOP example (I set the frame background to black to make it easier to see if the frame was expanding. You can change this back if you need to):
import tkinter as tk
root = tk.Tk()
root.title("manage your cashflow")
root.geometry("%dx%d" % (root.winfo_screenwidth(), root.winfo_screenheight()))
root.configure(bg='grey')
root.rowconfigure(1, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
tk.Button(root, text="the list of month", bg='#ebca87').grid(row=0, column=0, sticky='NEW')
tk.Button(root, text="the new month", bg='#ebca87').grid(row=0, column=1, sticky='NEW', pady=0)
frame2 = tk.Frame(root, bg='black')
frame2.grid(row=1, column=0, columnspan=2, sticky="NSEW")
frame2.rowconfigure(0, weight=1)
frame2.columnconfigure(0, weight=1)
frame2.columnconfigure(1, weight=1)
tk.Label(frame2, text="salary").pack()
salary_entry = tk.Entry(frame2)
salary_entry.pack()
root.mainloop()
Results:
For a more OOP geared option you can do something like this.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("manage your cashflow")
self.geometry("%dx%d" % (self.winfo_screenwidth(), self.winfo_screenheight()))
self.configure(bg='grey')
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
tk.Button(self, text="the list of month", bg='#ebca87').grid(row=0, column=0, sticky='NEW')
tk.Button(self, text="the new month", bg='#ebca87').grid(row=0, column=1, sticky='NEW', pady=0)
frame2 = Frame2()
frame2.grid(row=1, column=0, columnspan=2, sticky="NSEW")
class Frame2(tk.Frame):
def __init__(self):
super().__init__(bg='black')
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
tk.Label(self, text="salary").pack()
salary_entry = tk.Entry(self)
salary_entry.pack()
if __name__ == '__main__':
App().mainloop()

Related

Blank page when removing all mentions of grid()

I've switched from .grid() to .place() in my program, so I decided to remove a frame that contained the grid widgets:
BackButtonR = Button(registerPage, text="Back", command=lambda: show_frame(Menu))
BackButtonR.grid(row=0, column=0, sticky=W)
Button2F3 = Button(registerPage, text="Find")
Button2F3.grid(row=1, column=1)
Button3F3 = Button(registerPage, text="Calculate").grid(row=6, column=1)
LabelTitleF3 = Label(registerPage, text="Calculate Buy Price").grid(row=0, column=3)
label1F3 = Label(registerPage, text="Enter Ticker Symbol:").grid(row=1, column=0)
label2F3 = Label(registerPage, text="Expected CAGR").grid(row=2, column=0)
label3F3 = Label(registerPage, text="Years of Analysis").grid(row=3, column=0)
label4F3 = Label(registerPage, text="Expected PE Ratio").grid(row=4, column=0)
label5F3 = Label(registerPage, text="Desired Annual Return").grid(row=5, column=0)
entry1F3 = Entry(registerPage, width=7).grid(row=1, column=1, padx=0)
entry2F3 = Entry(registerPage).grid(row=2, column=1, pady=10, padx=0)
entry3F3 = Entry(registerPage).grid(row=3, column=1, pady=10, padx=0)
entry4F3 = Entry(registerPage).grid(row=4, column=1, pady=10, padx=0)
entry5F3 = Entry(registerPage).grid(row=, column=1, pady=10, padx=0)
But weirdly, when I rerun my program everything turns blank. This shouldn't happen, since I've removed any reference to .grid(), so the program should be working fine with .place(). Here is my full code:
print(220+135)
from tkinter import *
root = Tk()
root.title("Account Signup")
DarkBlue = "#2460A7"
LightBlue = "#B3C7D6"
root.geometry('350x230')
Menu = Frame(root)
loginPage = Frame(root)
registerPage = Frame(root)
for AllFrames in (Menu, loginPage, registerPage):
AllFrames.grid(row=0, column=0, sticky='nsew')
AllFrames.configure(bg=LightBlue)
def show_frame(frame):
frame.tkraise()
show_frame(Menu)
# ============= Menu Page =========
Menu.grid_columnconfigure(0, weight=1)
menuTitle = Label(Menu, text="Menu", font=("Arial", 25), bg=LightBlue)
menuTitle.place(x=130, y=25)
loginButton1 = Button(Menu, width=25, text="Login", command=lambda: show_frame(loginPage))
loginButton1.place(x=85, y=85)
registerButton1 = Button(Menu, width=25, text="Register", command=lambda: show_frame(registerPage))
registerButton1.place(x=85, y=115)
# ======== Login Page ===========
loginUsernameL = Label(loginPage, text='Username').place(x=30, y=60)
loginUsernameE = Entry(loginPage).place(x=120, y=60)
loginPasswordL = Label(loginPage, text='Password').place(x=30, y=90)
loginPasswordE = Entry(loginPage).place(x=120, y=90)
backButton = Button(loginPage, text='Back', command=lambda: show_frame(Menu)).place(x=0, y=0)
loginButton = Button(loginPage, text='Login', width=20).place(x=100, y=150)
# ======== Register Page ===========
root.mainloop()
Why is my program turning blank?
When you use pack and grid, these functions will normally adjust the size of a widget's parent to fit all of its children. It's one of the most compelling reasons to use these geometry managers.
When you use place this doesn't happen. If you use place to put a widget in a frame, the frame will not grow or shrink to fit the widget.
In your case you're creating Menu, loginPage and registerPage and not giving them a size so they default to 1x1 pixels. When you use place to add a widget to the frame, the frame will remain at 1x1 pixels, rendering it virtually invisible.
The solution is to either give these frames an explicit size, or add the frames to the window with options that cause them to fill the window.
For illustrative purposes I've changed the background color of the window to pink, and set the size of Menu to 200x200. As you can see in the following screenshot, the frame with the widgets is there, and becomes visible when you give it a larger size. Of course, one problem with place is it's up to you to calculate the appropriate size.
The better solution in this specific case would be to use the appropriate grid options to have the frames fill the window. You can do that by giving a weight to the row and column that the frames are in. Unused space in the parent frame will be allocated to the row and column with the widget.
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
Generally speaking, grid and pack are superior to place for implementing most layouts because they are able to automatically make all widgets fit into a window with very little work. With place it's up to you to do calculations for position and size, and to make sure that all ancestors are appropriately sized and are visible.
You need to call root.grid_rowconfigure(0, weight=1) and root.grid_columnconfigure(0, weight=1) so that the shown frame use all the space of root window, otherwise the size of those frames are 1x1.
Also Menu.grid_columnconfigure(0, weight=1) is useless because widgets inside Menu are using .place().

Python Tkinter Frame layout, weight, and resizing, Grid geometry manager

I guess I am not understanding something about the way Grid works...
My question seems related to this SO question:
Python tkinter Text widget fill fixed sized frame using grid
To which the answer was that weight needed to be given to an item in order have it take up the extra space, using the functions grid_columnconfigure() and grid_rowconfigure().
Below is the code for a simple example.
What I am trying to achieve is to be able to specify the size of frame0, have it be stuck to all sides of the root window, and then have the child frame, frame1 be stuck to frame0 on left, top, right, and to stretch/shrink when the main window of the app is resized.
So, initially, I would expect the application to launch at basically 500x350 size. frame1 would be as high as it naturally is and basically 500 pixels wide (minus a little for padding).
After acw1668's comment, I updated the code to configure root and set it's weight to 1. The size of Entry now varies to take up any horizontal space, just as I wanted. But the GUI still launches at "natural" size, though I have given an explicit size to frame0. I'm not understanding why the geometry manager does not treat 500x350 as the natural size and allocate that?
Can anybody see how to get the initial launch sized as expected?
#!/usr/bin/env python3
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Layout Problem")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
frame0 = ttk.Frame(root, padding=4, width=500, height=350)
frame0.grid_columnconfigure(0, weight=1)
frame0.grid_rowconfigure(0, weight=1)
frame0.grid(column=0, row=0, sticky=(W, N, E, S))
frame1 = ttk.Frame(frame0, padding=4)
frame1.grid_columnconfigure(1, weight=1)
frame1['borderwidth'] = 2
frame1['relief'] = "ridge"
frame1.grid(column=0, row=0, sticky=(W, N, E))
# add widgets to 'frame1'
ttk.Label(frame1, text="Label: ").grid(row=0, column=0, sticky=W)
entryValue = StringVar()
ttk.Entry(frame1, textvariable=entryValue)\
.grid(row=0, column=1, sticky=(W, E))
entryValue.set("Entry")
ttk.Button(frame1, text="Button")\
.grid(row=0, column=2, sticky=E)
root.mainloop()
For reference, below is the code after incorporating acw1668's suggestions.
The GUI now launches at expected size. There is sort of a secondary issue in that now the frame doesn't shrink below 500 px width (i.e., the Entry will not collapse back to natural size) though you can force the application window to be smaller. That's sort of a separate issue.
I can see there is some subtlety to this whole layout and resizing business and I've yet got a ways to go. ;)
Hopefully this question will have some pedagogic value to others following the same trail:
#!/usr/bin/env python3
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Layout Problem")
root.grid_columnconfigure(0, weight=1, minsize=500)
root.grid_rowconfigure(0, weight=1, minsize=350)
frame0 = ttk.Frame(root, padding=4)
frame0.grid(column=0, row=0, sticky=(W, N, E, S))
frame0.grid_columnconfigure(0, weight=1)
frame0.grid_rowconfigure(0, weight=1)
frame1 = ttk.Frame(frame0, padding=4)
frame1.grid_columnconfigure(1, weight=1)
frame1['borderwidth'] = 2
frame1['relief'] = "ridge"
frame1.grid(column=0, row=0, sticky=(W, N, E))
# add widgets to 'frame1'
ttk.Label(frame1, text="Label: ").grid(row=0, column=0, sticky=W)
entryValue = StringVar()
ttk.Entry(frame1, textvariable=entryValue)\
.grid(row=0, column=1, sticky=(W, E))
entryValue.set("Entry")
ttk.Button(frame1, text="Button")\
.grid(row=0, column=2, sticky=E)
root.mainloop()

Creating multiple widgets within a class frame

In simply trying to work on organizing my code, I've found online that it seems to be best to put much of your code into classes when needed. So in doing that, I'd figure that I'll try to create a frame class with create_labels and create_buttons methods.
My goal is to be able to create 2 or more seperate frames that are similar in style (hence why I find it best to make a frame class). Then, using methods, create labels, buttons, and other widgets and allow them to move around with ease within their respective frames.
So here's my code:
import tkinter as tk
window = tk.Tk()
class MyFrame(tk.Frame):
def __init__(self, parent, **kwargs):
tk.Frame.__init__(self, parent)
self.parent = parent
self.layout(**kwargs)
def labels(self, text, **kwargs):
tk.Label.__init__(self, text=text)
self.layout(**kwargs)
def buttons(self, text, command, **kwargs):
tk.Button.__init__(self, text=text, command=command)
self.layout(**kwargs)
def layout(self, row=0, column=0, columnspan=None, row_weight=None, column_weight=None, color=None, sticky=None, ipadx=None, padx=None, ipady=None, pady=None):
self.grid(row=row, column=column, columnspan=columnspan, sticky=sticky, ipadx=ipadx, padx=padx, ipady=ipady, pady=pady)
self.grid_rowconfigure(row, weight=row_weight)
self.grid_columnconfigure(column, weight=column_weight)
self.config(bg=color)
frame_1 = MyFrame(window, row=0, column=0, sticky="news", color="pink")
frame_1.buttons("Btn_1/Frme_1", quit, row=0, column=0)
frame_1.buttons("Btn_2/Frme_1", quit, row=0, column=1)
frame_2 = MyFrame(window, row=1, column=0, sticky="news", color="green")
frame_2.buttons("Btn_1/Frme_2", quit, row=0, column=0)
frame_2.buttons("Btn_2/Frme_2", quit, row=0, column=1)
window.grid_columnconfigure(0, weight=1)
window.grid_columnconfigure(1, weight=1)
window.grid_rowconfigure(1, weight=1)
window.grid_rowconfigure(0, weight=1)
window.mainloop()
Now I think a problem of mine is during the __init__ method because there should be 2 frames and 2 buttons per frame. However, there's no errors which makes it harder to find out for sure of that's why only the latest buttons and frames exist. I don't even think it's a case of one frame or widget 'covering' another. I think the second frame/widgets seem to be overwriting the first frame/widgets.
Any help is appreciated.
The issue lies with your layout function. Both the frames are being grided on the row=0 and column=0, as you are not passing any specific row and column to the function. Hence, the overwriting of the frames can be seen.
Another issue (possible) in your code is that the frame_1 and frame_2 buttons do not belong to the Frame widget but to the root window

Tkinter: Too much space between label and button frame

I'm pretty new to Tkinter and I build a little window with different widgets.
My Code looks like this:
import tkinter as tk
from tkinter import ttk
class Application(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.master.geometry("800x600")
self.master.title("Tkinter Sandbox")
self.master.grid_rowconfigure(0, weight=1)
self.master.grid_columnconfigure(1, weight=1)
self._create_left_frame()
self._create_button_bar()
self._create_label_frame()
def _create_left_frame(self):
frame = tk.Frame(self.master, bg="red")
tree_view = ttk.Treeview(frame)
tree_view.column("#0", stretch=tk.NO)
tree_view.heading("#0", text="Treeview")
tree_view.pack(fill=tk.Y, expand=1)
frame.grid(row=0, column=0, rowspan=2, sticky=tk.N + tk.S)
def _create_button_bar(self):
frame = tk.Frame(self.master, bg="blue")
button_run_single = tk.Button(frame, text="Button 1")
button_run_all = tk.Button(frame, text="Button 2")
button_details = tk.Button(frame, text="Button 3")
button_run_single.grid(row=0, column=0)
button_run_all.grid(row=0, column=1, padx=(35, 35))
button_details.grid(row=0, column=2)
frame.grid(row=0, column=1, sticky=tk.N)
def _create_label_frame(self):
frame = tk.Frame(self.master, bg="blue")
name_label = tk.Label(frame, text="Label 1")
performance_label = tk.Label(frame, text="Label 2")
name_entry = tk.Entry(frame)
performance_entry = tk.Entry(frame)
name_label.grid(row=0, column=0)
name_entry.grid(row=0, column=1)
performance_label.grid(row=1, column=0)
performance_entry.grid(row=1, column=1)
frame.grid(row=1, column=1)
if __name__ == '__main__':
root = tk.Tk()
app = Application(root)
app.mainloop()
Between the three buttons and the label + entry frame is a huge space. I want the button and label + entry frame right under each other, without the huge space but the treeview should also expand vertically over the whole application window.
I think the problem might be my row and column configuration but I don't know how to solve this problem.
The way you've structured your code makes it hard to see the problem. As a good general rule of thumb, all calls to grid or pack for widgets within a single parent should be in one place. Otherwise, you create dependencies between functions that are hard to see and understand.
I recommend having each of your helper functions return the frame rather than calling grid on the frame. That way you give control to Application.__init__ for the layout of the main sections of the window.
For example:
left_frame = self._create_left_frame()
button_bar = self._create_button_bar()
label_frame = self._create_label_frame()
left_frame.pack(side="left", fill="y")
button_bar.pack(side="top", fill="x")
label_frame.pack(side="top", fill="both", expand=True)
I used pack here because it requires less code than grid for this type of layout. However, if you choose to switch to grid, or wish to add more widgets to the root window later, you only have to modify this one function rather than modify the grid calls in multiple functions.
Note: this requires that your functions each do return frame to pass the frame back to the __init__ method. You also need to remove frame.grid from each of your helper functions.
With just that simple change you end up with the button bar and label/entry combinations at the top of the section on the right. In the following screenshot I changed the background of the button_bar to green so you can see that it fills the top of the right side of the UI.
You need to change line
self.master.grid_rowconfigure(0, weight=1)
to
self.master.grid_rowconfigure(1, weight=1)
so that the second row takes all the space. Then you need to stick widgets from the label frame to its top by adding sticky parameter to the grid call in _create_label_frame:
frame.grid(row=1, column=1, sticky=tk.N)
I prefer to use the Pack Function since it gives a more open window - its easy to configure. When you use Pack() you can use labels with no text and just spaces to create a spacer, by doing this you won't run into the problem your facing.

Python - Center contents (not window) with Tkinter?

When I search Google everything comes back for centering the window when using Tkinter. What I'd like to do is center the contents of the window. I am thinking this might not be possible since you place everything at a specific x,y position. So when the window opens let say it is 500x500 and I place buttons and labels and stuff. When the user expands the window full screen or by dragging the corner can the contents recenter inside the window?
Of course it's possible. You have complete control over what goes in a window, and you don't have to place things at specific x,y positions. Tkinter has three geometry managers - pack, place and grid, plus you can also use the text widget and canvas as containers for other widgets. Except for the text widget, all of the rest make it pretty easy to put something in the middle of something else.
Here's a couple of the most obvious examples:
import Tkinter as tk
class Example1(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.label = tk.Label(self, text="Hello, expample1")
self.label.place(relx=.5, rely=.5, anchor="c")
class Example2(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.label = tk.Label(self, text="Hello, example2")
self.label.grid(row=1, column=1, sticky="nsew")
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(2, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(2, weight=1)
if __name__ == "__main__":
root = tk.Tk()
top = tk.Toplevel()
Example1(root).pack(fill="both", expand=True)
Example2(top).pack(fill="both", expand=True)
root.geometry("300x300+100+100")
top.geometry("300x300+450+100")
root.mainloop()
Okay, I managed to figure this one out. I am posting my answer in hopes it helps others.
import Tkinter as tk
root=tk.Tk()
f1 = tk.Frame(width=800, height=600, background="red")
f2 = tk.Frame(width=775, height=575, background="blue")
f1.pack(fill="both", expand=True, padx=20, pady=20)
f2.place(in_=f1, anchor="c", relx=.5, rely=.5)
#f2.rowconfigure(0, weight=1)
#f2.columnconfigure(0, weight=1)
l1 = tk.Label(f2, text="First")
l1.place(x=0, y=0)
l2 = tk.Label(f2, text="Second")
l2.place(x=100, y=300)
root.mainloop()
You can simply use the grid manager and change the column weight. Changing the weight of a column will change how much it expands when the window expands, so you can do:
from tkinter import *
root = Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.rowconfigure(2, weight=1)
root.columnconfigure(2, weight=1)
contents = Frame(root)
contents.grid(row=1, column=1)
# Insert main contents into frame "contents"
This will result in a simple centring effect as the columns and rows either side of the main content are expanding equally to fill the window.

Categories

Resources