您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Python中使用Tkinter在更改消息和更改按钮状态的情况下创建自定义消息框

如何在Python中使用Tkinter在更改消息和更改按钮状态的情况下创建自定义消息框

尝试开发自己的对话框。例:

import Tkinter as tk


class CustomDialog(tk.Toplevel):
    def __init__(self, title, message, command1=self.ok, command2=self.ok, command3=self.ok, buttontext1="button1", buttontext2="button2", buttontext3="button3"):
        self.base = tk.Toplevel()
        self.base.title(title)
        self.label = tk.Label(self.base, text=message)
        self.label.pack()
        self.label.grid(row=0, column=0, columnspan=3, sticky=N)
        self.button1 = tk.Button(self.base, text=buttontext1, command=command1)
        self.button1.pack()
        self.button1.grid(row=1, column=0, sticky=N)
        self.button2 = tk.Button(self.base, text=buttontext2, command=command2)
        self.button2.pack()
        self.button2.grid(row=1, column=1, sticky=N)
        self.button3 = tk.Button(self.base, text=buttontext3, command=command3)
        self.button3.pack()
        self.button3.grid(row=1, column=2, sticky=N)
    def ok(self, event=None):
        self.destroy()
    def baseconfig(self, option, value):
        self.base[option] = value
    def labelconfig(self, option, value):
        self.label[option] = value
    def buttonconfig(self, number, option, value):
        exec "self.button%s[option] = value" % str(number)

def customDialog(title, message, command1=self.ok, command2=self.ok, command3=self.ok, buttontext1="button1", buttontext2="button2", buttontext3="button3", button1ret=None, button2ret=None, button3ret=None):
    def _button1press():
        command1()
        return button1ret
    def _button2press():
        command2()
        return button2ret
    def _button3press():
        command3()
        return button3ret
    dialog = CustomDialog(title, message, _button1press, _button2press, _button3press, buttontext1, buttontext2, buttontext3)

要禁用第一个按钮,请调用self.buttonconfig(1, state, DISABLED)(1表示按钮的编号)。要启用它,请使用self.buttonconfig(1, state, NORMAL)

python 2022/1/1 18:38:08 有543人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶