我收到错误TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given
,但我不知道为什么吗?
我有一个窗口,然后调用一个函数来打开第二个窗口。第二个窗口使用类并引发错误。这是导致错误的代码行:
def openadmin():
class SCapp(tk.Toplevel(window)):
def __init__(self, *args, **kwargs):
tk.Toplevel.__init__(self, *args, **kwargs)
(显然,这不是全部代码,只是引发TypeError的那一部分)
(“窗口”是第一个调用函数openadmin()
的窗口以打开第二个窗口)
任何帮助将不胜感激,因为这是我的课程!
编辑:
忽略上一个代码^^
这是第二个使用框架的窗口的工作代码的基本版本(我已经应用了一些更改):
import tkinter as tk
class SCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,): #list of frames
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label1 = tk.Label(self,text='This is the start page of 2nd window')
label1.pack()
def openadmin():
if __name__ == "__main__":
app = SCapp()
app.title("SC App")
app.attributes("-topmost", True)
app.mainloop()
这是第一个窗口(在单独的文件中),应该打开前一个窗口:
import tkinter as tk
from testcode import *
def onClick():
if False:
return #some code is here
else:
openadmin()
def WindowOne():
global entid, entpassword
window = tk.Tk()
window.wm_title("Window 1")
label1 = tk.Label(text='This is the first window')
label1.pack()
enter = tk.Button(window, text = 'Click', command = onClick)
enter.pack()
window.mainloop()
WindowOne()
当它们在同一个文件中时,它可以正常工作,但是当它们在单独的文件中时(如我需要它们),单击按钮时什么也没有发生。