创建更改菜单的按钮

2024-04-25 04:33:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Python3中的类创建一个简单的菜单。我曾经尝试过用“其他菜单”按钮创建一个初始菜单,然后单击它就会转到另一个带有“这是其他菜单”按钮的菜单。你知道吗

不过,它目前只是在启动时显示两个菜单。你知道吗

最终我想有一个12个按钮的初始菜单,然后将重定向到其他菜单。我可以通过创建一个新窗口来实现这一点,但我更希望它保持在同一个窗口中。你知道吗

我在下面做错了什么?你知道吗

#import the tkinter module
from tkinter import *

#create a new class
class Try(Frame):
    def __init__(self, master):
        super(Try, self).__init__(master)
        self.grid(row = 2, sticky = W+E+N+S)
        #,padx=300
        self.create_widgets()

    def create_widgets(self):

        #create buttons
        self.bttn1 = Button(self, text = 'Other Menu')
        self.bttn1['command'] = self.create_widgets2()
        self.bttn1.grid(sticky = W+E+N+S) 

    def create_widgets2(self):

        #create buttons
        self.bttn2 = Button(self, text = 'This is the other menu')
        self.bttn2.grid(sticky = W+E+N+S) 

#main
root = Tk()
root.title('Editor')
root.geometry()
root.configure(background='black')

app = Try(root)

root.mainloop()

Tags: theimportselfinittkinterdefcreate菜单
1条回答
网友
1楼 · 发布于 2024-04-25 04:33:43

分配按钮的command选项时,您正在调用create_widgets2方法中的create_widgets方法:

self.bttn1['command'] = self.create_widgets2()
#                                           ^^

您需要删除括号,以便command选项只分配给函数对象:

self.bttn1['command'] = self.create_widgets2

相关问题 更多 >