我一直在进行Gui的多窗口任务,但是Tkinter似乎没有Tk。我的完整错误是
mainline
我的代码是
Traceback (most recent call last):
File "/Users/connorsmacbook/PycharmProjects/2.8/2.8 Internal/TextTypers 2.2.py", line 6, in <module>
class TextTypers(tk.TK):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2101, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'TK'
如果有向导可以帮助我,我将不胜感激:)。
答案 0 :(得分:2)
行
tk=Tk()
创建一个名为tk的Tk()实例。
创建类时
class TextTypers(tk.TK):
您正在尝试从实例tk继承一个名为TK的属性。
通常,我不会为根窗口使用名称tk
,因为tk通常用作tkinter模块的别名。
我认为您追求的是这样的:
import tkinter as tk
# Classes
class TextTypers(tk.Tk):
def __init__(self, *args, **kwargs): # Runs when our class is called and allows almost anything to be passed
tk.Tk.__init__(self, *args, **kwargs) # Initialise Tk
window = tk.Frame(self) # Creates the container the windows/frames will populate
window.pack()
self.frames = {} # Creates a dictionary for the frames
frame = MenuScreen(window, self)
self.frames[MenuScreen] = frame
frame.grid(row=0, column=0, sticky="nswe")
self.show_frame(MenuScreen) # Shows the menu screen as this is initialising
def show_frame(self, cont):
frame = self.frames[cont] # Grabs value of self.frames and puts in in frame
frame.tkraise() # Raises frame to the front
class MenuScreen(tk.Frame): # Inherits everything from the frame
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent) # Inherits from main class
label = tk.Label(self, text="Menu")
label.pack()
run = TextTypers()
run.mainloop()
看看Best way to structure a tkinter application,您会发现一些建议和讨论吗。