user1546859
user1546859

Reputation: 240

Python GUI Programming Using Tkinter

So I am creating a game, and now I have decided to add a GUI. The GUI code is shown as follows:

master = Tk()
master.wm_title("Main Menu")

a = Button.grid(master, text = "DOMINATE!", command = difficulty)
a.grid(row = 0, column = 0)
a.pack()
mainloop()

b = Button.grid(master, text = "Patch Notes", command = changelog)
b.grid(row = 0, column = 1)
b.pack()
mainloop()

c = Button.grid(master, text = "Credits", command = credit)
c.grid(row = 1, column = 0)
c.pack()
mainloop()

d = Button.grid(master, text = "Rules", command = rules)
d.grid(row = 1, column = 1)
d.pack()
mainloop()

e = Button.grid(master, text = "Quit", command = exit)
e.grid(row = 2, column = 0)
e.pack()
mainloop()

I have already imported the function * from Tkinter and this is just a snippet of my 1200 lines of code. When I try to run it, I get this error:

TypeError: unbound method grid_configure() must be called with Button instance as first argument (got Tk instance instead)

(Python is kind of a new language for me so sorry for missing anything obvious or stupid. I am also kind of new to programming).

Upvotes: 0

Views: 734

Answers (3)

trixo
trixo

Reputation: 11

The main error comes from the 'master' which is not a Button instance in :

a = Button.grid(master, text = "DOMINATE!", command = difficulty)

do instead :

a = Button(master, text = "DOMINATE!", command = difficulty)
Button.grid(master.a,row=0,column=0,rowspan=1,columnspan=1,sticky='wens')

the line for grid placement use the Button superclass grid method for Tkinter Old-style classes. but you don't seem to need that so you could do :

a.grid(master.self,row=0,column=0,rowspan=1,columnspan=1,sticky='wens')

Doing :

a = Button(master, text = "DOMINATE!", command = difficulty).grid(row=0,column=0,rowspan=1,columnspan=1,sticky='wens') 

"a" will not be a Button instance callable later, as it will be a grid instance equal to NoneType. Split both creation and placement.

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385800

The error "unbound method grid_configure() must be called with Button instance..." comes from lines that look like this:

a = Button.grid(...)

What the above does is try to call the grid method on the Button class, rather than on an instance of the Button class. Generally speaking, any "unbound method" error means exactly that -- you're trying to call a method on a class rather than an instance.

Instead, you must first create an instance of the Button class, then call grid on the instance. For example:

a = Button(...)
a.grid(...)

Also, using both pack and grid for the same widget makes no sense. Use one or the other. If you use grid you don't need to call pack.

Finally, mainloop is designed to be called exactly once after all your widgets have been created.

On a related note, in my experience GUI code is much easier to maintain if you put all your layout code (grid, pack and place) together. Not necessarily all together for the entire app, but at least grouped by the containing window. For example, instead of this:

a = Button(...)
a.grid(...)
b = Button(...)
b.grid(...)
...

... I recommend doing it like this:

a = Button(...)
b = Button(...)
...
a.grid(...)
b.grid(...)

This sort of code organization makes it easier to spot layout problems such as having multiple items in the same row or column, etc.

Upvotes: 0

mgilson
mgilson

Reputation: 309821

You want something like:

a = Button(master, text = "DOMINATE!", command = difficulty)
a.grid(row=...,column=...)

Also, don't do

a.grid(...)
a.pack(...)

Using pack and grid on the same widget (or even within the same widget) will likely leave your computer sitting there forever trying to negotiate a reasonable layout between the two geometry managers. Either use only pack, or only grid.

Finally, you only need the last mainloop(). You should remove all the others -- although if you have 1200 lines of code, I suspect that correcting these things will raise other errors.

Upvotes: 1

Related Questions