Reputation: 21
I'm trying to create a child widget based on Entry widget. Unfortunately it's not working as I think it should. From the following code:
import tkinter as tk
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "SOLVE"
self.hi_there.grid(row=1, column=1)
self.QUIT = tk.Button(
self, text="QUIT", fg="red", command=self.master.quit)
self.QUIT.grid(row=1, column=2)
# self.number_box = tk.Entry(self) # This is working
self.number_box = NumberBox(self) # this is not working
self.number_box.grid(row=2, column=1)
class MainWindow():
def __init__(self):
#self.root = tk.Tk()
self.app = Application()
self.app.master.title("SUDOKU")
self.app.master.minsize(width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
self.app.master.maxsize(width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
self.app.mainloop()
class NumberBox(tk.Entry):
def __init__(self, master=None, cnf={}, **kw):
super().__init__(cnf, kw)
#self.text = tk.StringVar()
window = MainWindow()
I get an error:
Traceback (most recent call last):
File "E:/workspace/python/sudoku/gui/guitk.py", line 42, in <module>
window = MainWindow()
File "E:/workspace/python/sudoku/gui/guitk.py", line 28, in __init__
self.app = Application()
File "E:/workspace/python/sudoku/gui/guitk.py", line 11, in __init__
self.createWidgets()
File "E:/workspace/python/sudoku/gui/guitk.py", line 22, in createWidgets
self.number_box.grid(row=2, column=1)
File "C:\Python34\lib\tkinter\__init__.py", line 2057, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by packenter code here
When I use Entry class directly (not the NumberBox, see commented line), code is working. What do I wrong with inheritance from Entry that my class is not working properly.
Upvotes: 1
Views: 2192
Reputation: 87084
Your super()
call looks wrong. You need to pass the parent widget and pass unpacked keyword arguments, like this:
super().__init__(master, cnf, **kw)
Upvotes: 2