Home > Back-end >  How do you resize the window size of a tkinter application made using the class method?
How do you resize the window size of a tkinter application made using the class method?

Time:11-14

Hey guys so I am trying to resize my tkinter window using the geometry function if the master window.

I am able to resize it when I tried to do that without creating a class.

But when I tried to resize the window made by the class method I came across a method where the master was resized before passing it as a parameter in the class initialization.

But when I tried to do that without passing the master without passing it as a parameter my interpreter crashes.

What I want to know is what I am doing is it correct?

If not please help me understand why is the interpreter crashing.

Here is the code that I Tried to run.

class App(Tk):
    def __init__(self):
        self.geometry('300x300')
        #self.tk.Tk.geometry('300x300')   Tried this but it didn't do anything.
        Label(text='Post Navigation Button').grid(row=0,column=0)
        self.listbox = Listbox(selectmode='multiple',height=len(x))
        for each_item in range(len(x)):
            self.listbox.insert(END, x[each_item])
            self.listbox.itemconfig(each_item, bg = "lime")
        self.listbox.grid(row=0,column=1)
        self.tag = Text(height=1,width=100)
        self.tag.grid(row=0,column=2)
        Button(text='click me',command=self.submit).place(x=500,y=50)
    
    def submit(self):
        text = self.tag.get('0.0',END)
        selection = [self.listbox.get(i) for i in self.listbox.curselection()]
        print(text.strip(),','.join(selection))
        
app = App()
mainloop()

I don't want to create a master and pass it as a parameter like this.

root = tk.Tk()
root.geometry('300x300')
app = App(root)
mainloop()

So I just want to know the way to resize the the window from inside the class.

I don't know how correct my code is. So any help is welcome.

CodePudding user response:

The geometry has to be used on the TK() object, not on your class based on it. To do this, you have to get the parent of your class:

def __init__(self):
    top=self.winfo_toplevel() #Flexible Toplevel of the window
    top.geometry("300x300")

The methods . rowconfigure() and .columnconfigure() also have to applied to top and not self.

CodePudding user response:

Your code has a couple flaws:

  1. you need to instanciate the Tk class with super().__init__() at the very beginning of your init to use its methods and attributes
  2. The first arguments for the widgets should be the master, so you need to pass it via self
  3. You didnt define your list x
  4. mainloop is a function of your object app, so call it with app.mainloop()

Working example:

from tkinter import Tk, Label, Listbox, END, Button, Text

class App(Tk):
    def __init__(self):
        super().__init__()
        self.geometry('300x300')
        #self.tk.Tk.geometry('300x300')   Tried this but it didn't do anything.
        Label(self, text='Post Navigation Button').grid(row=0,column=0)
        x = [1,2,3]
        self.listbox = Listbox(self, selectmode='multiple',height=len(x))
        for each_item in range(len(x)):
            self.listbox.insert(END, x[each_item])
            self.listbox.itemconfig(each_item, bg = "lime")
        self.listbox.grid(row=0,column=1)
        self.tag = Text(self, height=1,width=100)
        self.tag.grid(row=0,column=2)
        Button(self, text='click me',command=self.submit).place(x=500,y=50)
    
    def submit(self):
        text = self.tag.get('0.0',END)
        selection = [self.listbox.get(i) for i in self.listbox.curselection()]
        print(text.strip(),','.join(selection))
        
app = App()
app.mainloop()
  • Related