我目前正在开发一个tkinter项目,我正在使用Canvas小部件更改框架。到目前为止我有这个:
class StartPage(Frame):
#defines what is supposed to be running when the class is called
def __init__(self, parent, controller):
Frame.__init__(self, parent)#parent class = USBSecure
Frame.configure(self, background = 'dark grey')
self.frames = {}
#creates button to get to the help page
helppage = Canvas(self, width = 200, height = 50, background = mycolor)#, font = "Helvetica 16 bold italic", fg = 'silver')
helppage.create_text(100, (50/2), text = 'Help',font = "Helvetica 16 ")
#creates endProgram button
endPrgm = Canvas(self, width = 200, height = 50, background = mycolor)#, font = "Helvetica 16 bold italic", fg = 'silver')
endPrgm.create_text(100, (50/2), text = 'End the Program',font = "Helvetica 16 ")
#binds the buttons
helppage.bind('<Button-1>', self.screenChanger)
endPrgm.bind('<Button-1>', USBSecure.endProgram)
#grids the buttons
helppage.grid(row=0, column = 0, sticky='nsew')
endPrgm.grid(row=1, column = 0, sticky="nsew")
#help screen
def screenChanger(self, event):
for screen in (HelpPage,LogfilePage):
frame = screen(container, self)
self.frames[screen] = frame
frame.grid(row=0, column = 0, sticky="nsew")
frame = self.frames[HelpPage]
frame.tkraise()
上面的代码完美无缺,但是......
我正在尝试让屏幕切换器功能接受另一个变量,例如pageNum
,如下所示:
#help screen
def screenChanger(self, event, pageNum):
for screen in (HelpPage,LogfilePage):
frame = screen(container, self)
self.frames[screen] = frame
frame.grid(row=0, column = 0, sticky="nsew")
if pageNum == 1:
frame = self.frames[HelpPage]
frame.tkraise()
然后我像这样传递pageNum
:
helppage.bind('<Button-1>', self.screenChanger(1))
然而,当我这样做时,我得到一个看起来像
的错误TypeError: screenChanger() missing 1 required positional argument: 'pageNum'
>>>
我尝试了helppage.bind('<Button-1>', self.screenChanger(pageNum=1))
,但这给了我类似的错误
TypeError: screenChanger() missing 1 required positional argument: 'event'
>>>
为什么会发生这种情况,我该如何解决?
编辑:
所以我考虑到了Vikas Damodar所说的变化,所以现在我有了这个。
#binds the buttons
helppage.bind('<Button-1>', self.screenChanger(Page = HelpPage))
logfile.bind('<Button-1>', self.screenChanger(Page = LogfilePage))
endPrgm.bind('<Button-1>', USBSecure.endProgram)
#grids the buttons
helppage.grid(row=0, column = 0, sticky='nsew')
logfile.grid(row=1, column = 0, sticky='nsew')
endPrgm.grid(row=2, column = 0, sticky="nsew")
#help screen
def screenChanger(Page, self, event=None):
frame = Page(container, self)
self.frames[Page] = frame
frame.grid(row=0, column = 0, sticky="nsew")
frame = self.frames[Page]
frame.tkraise()
但是,我现在收到此错误:
TypeError: screenChanger() got multiple values for argument 'Page'
答案 0 :(得分:0)
在您的方法screenChanger
中,您提供了2个参数。 event
和pageNum
,所以当您调用该函数时,您必须传递两个参数,否则您可以给出一些默认值。
从您的问题我认为您只想传递{{1 pageNum
我无法看到你在任何地方使用过。所以你可以尝试这样,它会为事件提供默认值。
event
然后你可以打电话
你的方法如def screenChanger(self, event=None, pageNum):
for screen in (HelpPage,LogfilePage):
frame = screen(container, self)
.....