0

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

dead day
  • 27
  • 6
  • can you provide an image of what you want to do? like how it should look – Matiiss Sep 22 '21 at 17:58
  • actually, I made a white frame and I want to have that frame all over the window? did you get it? normally sticky='news' fill the window but in my code, it's not working. there is some space which is not covered – dead day Sep 22 '21 at 18:02
  • please provide a complete [mre], for example how do you event create those buttons – Matiiss Sep 22 '21 at 18:05
  • The code you posted doesn't match the screenshot. You mention buttons but your code doesn't create any buttons. When I run your code after adding enough extra code to make it work, the frame completely fills the root window just as I would expect. – Bryan Oakley Sep 22 '21 at 18:13
  • sorry for that.. i ve edited it.. please check it once again – dead day Sep 22 '21 at 18:24
  • probably remove this line `Grid.rowconfigure(root,0,weight=1)`, also why don't you use sth like `root.rowconfigure(0, weight=1)` – Matiiss Sep 22 '21 at 18:33

1 Answers1

2

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.

  1. 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).

  2. 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.

  3. 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.

  4. 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:

enter image description here

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • I really appreciate your help. if __name__ == '__main__': App().mainloop(). what does that really do – dead day Sep 23 '21 at 15:52
  • @deadday this is a really common question. Please take a look at this link for good explanation https://stackoverflow.com/questions/419163/what-does-if-name-main-do – Mike - SMT Sep 23 '21 at 16:02