2

I am learning how to use Tkinter to make GUI apps and I can not figure out why I can not pass a width and height argument through my class __init__ function.

Edit: Sorry, got excited. What it's doing is telling me that I'm passing too many arguments. And no matter how I rearrange the parameters of the class __init__ and Frame __init__, nothing changes. It's either too many, or not enough.

Edit_2: Alright, this runs without error. But it's still not resizing the frame.

Here is what I am working with.:

#!/usr/bin/python

from Tkinter import *


class App(Frame):
    def __init__(self, parent, width, height):
        Frame.__init__(self, parent)
        self.parent = parent
        self.width = width
        self.height = height
        self.initUI()

    def initUI(self):

        self.parent.title("App")
        self.grid()

def main():

    root = Tk()
    app = App(root, 300, 250)
    root.mainloop()

if __name__ == '__main__':
    main()
3
  • 2
    Your code as listed works for me. Can you add the traceback of the exception you're getting to your question? Commented Jan 26, 2016 at 0:51
  • Hmm. It's running without error like this. I tore out a few methods used for widget placement to keep it short. But apart from it running, it's still not executing the task I'm trying to perform. For me, the window is barely visible when it inits. Then I have to resize manually. Commented Jan 26, 2016 at 0:57
  • 2
    You have to set main window size (root is main window) or frame size. But usin self.width will not change frame size. You have to use self.config(width=new_width) Commented Jan 26, 2016 at 1:06

1 Answer 1

4

self.width = width doesn't change frame size because frame use different method to change size.

First method: you can use Frame.__init__.

Frame.__init__(self, parent, width=width, height=height)

See:

from tkinter import *


class App(Frame):
    def __init__(self, parent, width, height):
        Frame.__init__(self, parent, width=width, height=height)
        self.parent = parent
        self.initUI()

    def initUI(self):

        self.parent.title("App")
        self.grid()

def main():

    root = Tk()
    app = App(root, 300, 250)
    root.mainloop()

if __name__ == '__main__':
    main()

Second method: you can use self.config(key=value) or self["key"] = value

self.config(width=width, height=height)

or

self["width"] = width
self["height"] = height

See:

from tkinter import *


class App(Frame):
    def __init__(self, parent, width, height):
        Frame.__init__(self, parent)
        self.parent = parent

        #self.config(width=width, height=height)

        self["width"] = width
        self["height"] = height

        self.initUI()

    def initUI(self):

        self.parent.title("App")
        self.grid()

def main():

    root = Tk()
    app = App(root, 300, 250)
    root.mainloop()

if __name__ == '__main__':
    main()

BTW: Frame.__init__ creates self.master and assigns parent to self.master so you can use self.master instead of self.parent


BTW: you can create window without Frame

from tkinter import *


class App(Tk):

    def __init__(self, width, height): # no need "parent"
        Tk.__init__(self) # no need "parent"

        self["width"] = width
        self["height"] = height

        # OR         
        #self.geometry("%dx%d" % (width, height))
        #self.geometry("%dx%d+%d+%d" % (width, height, x, y))

        self.initUI()

    def initUI(self):

        self.title("App") # no need "parent"
        # no need self.grid()


def main():

    App(300, 250).mainloop()

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

8 Comments

Oh lovely!! Absolutely! Thank you! It works like a charm now!
Frame.__init__ creates self.master and you can use it instead of self.parent
I had noticed when I was adding widget methods into this class that I didn't really have to designate the window, like root or self.parent, into the .grid function. Is that normal or is that because I'm using Frame.__init__?
command root = Tk() creates and displays main window. Frame is only "invisible" (without border) object to group other widgets. You can create class App(Tk) and add widgets directly to main window (without Frame)
you don't need Frame at that moment. You will need Frame when you need to group widgets and show/hide frame with all widgets at once, or you need to use pack() inside one frame and grid() inside another frame (don't mix pack and grid in one frame or window). An when you need to create own widget - class TwoButonsAndOneLabel(Frame)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.