0

For a beginner in Tkinter, and just average in Python, it's hard to find proper stuff on tkinter. Here is the problem I met (and begin to solve). I think Problem came from python version.

I'm trying to do a GUI, in OOP, and I got difficulty in combining different classes.

Let say I have a "small box" (for example, a menu bar), and in want to put it in a "big box". Working from this tutorial (http://sebsauvage.net/python/gui/index.html), I'm trying the following code

#!usr/bin/env python3.5
# coding: utf-8   
import tkinter as tki

class SmallBox(tki.Tk):
    def __init__(self,parent):
        tki.Tk.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text="small box")
        self.box.grid()
        self.graphicalStuff = tki.Entry(self.box) # something graphical
        self.graphicalStuff.grid()

class BigBox(tki.Tk):
    def __init__(self,parent):
        tki.Tk.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text='big box containing the small one')
        self.graphStuff = tki.Entry(self.box) # something graphical
        self.sbox = SmallBox(self)
        self.graphStuff.grid()
        self.box.grid()
        self.sbox.grid()

But I got the following error.

File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TypeError: create() argument 1 must be str or None, not BigBox  
Guilhem L.
  • 421
  • 2
  • 8

2 Answers2

1

The tutorial you are using has an incorrect example. The Tk class doesn't have a parent.

Also, you must only create a single instance of Tk (or subclass of Tk). Tkinter widgets exist in a tree-like hierarchy with a single root. This root widget is Tk(). You cannot have more than one root.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

The code looks quite similar at this one : Best way to structure a tkinter application

But there is one slight difference, we're not working on Frame here. And the error asks for a problem in screenName, etc. which, intuitively, looks more like a Frame.

In fact, I would say that in Python3 you can't use anymore the version of the first tutorial and you have to use Frame, and code something like that :

#!usr/bin/env python3.5
# coding: utf-8
import tkinter as tki
class SmallBox(tki.Frame):
    def __init__(self,parent):
        tki.Frame.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text="small box")
        self.box.grid()
        self.graphicalStuff = tki.Entry(self.box) # something graphical
        self.graphicalStuff.grid()

class BigBox(tki.Frame):
    def __init__(self,parent):
        tki.Frame.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text='big box containing the small one')
        self.graphStuff = tki.Entry(self.box) # something graphical
        self.sbox = SmallBox(self)
        self.graphStuff.grid()
        self.box.grid()
        self.sbox.grid()

if __name__ == '__main__':
    tg = BigBox(None)
    tg.mainloop()

We don't find (especially for French people, or maybe people not "natural" in english) many examples and docs, and the one I use is quite common, so maybe it will be useful to someone.

Community
  • 1
  • 1
Guilhem L.
  • 421
  • 2
  • 8
  • Your example is good, but you should explicitly create a root window, and pass that in as the first parameter to `BigBox` (eg: `root=Tk(); tg = BigBox(root)`) – Bryan Oakley Feb 14 '17 at 16:22
  • Ok, I keep it. So if I understand it, when I change for Tk to Frame, I have now to create a Tk() object and give it as a parent to my major frame. – Guilhem L. Feb 15 '17 at 13:54