如何在 Python 中使用 Tkinter 创建自定义消息框并更改消息和更改按钮状态 [英] How to create a custom messagebox using Tkinter in Python with changing message and changing button state

查看:95
本文介绍了如何在 Python 中使用 Tkinter 创建自定义消息框并更改消息和更改按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 root window 顶部创建一个消息窗口,它看起来与这个 链接.带有消息和非活动按钮的消息窗口,直到代码的某些部分完成,然后显示带有活动按钮的消息.这就是代码结构的样子(我已经削减了具体细节以明确我的代码流程的样子)

I wanted to cerate a message window on top of root window which looks similar from this link. A message window with a message and an inactive button untill some portion of the code are done and then to display a message with an active button. This is how the code structure looks like(I have cut through specific details to just clear the point how my code flow looks like)

第二个窗口不起作用,而是出现在函数的末尾.有人可以帮忙吗.

The second window is not working, instead the window appears at the end of the function. Can somebody help.

from Tkinter import *
import tkMessageBox

def a():
    return

def b():
    . . .
    . . .
    msg_window = Toplevel()
    msg_window.grapset()
    msg_window.title( ___ )
    msg_geometry( ___ )
    msgLabel = Label(window)
    msgLabel["text"]="Processing...."
    msgLabel.pack()
    btn = Button(msg_window, text="Ok", command=msg_window.destroy())
    btn.pack()
    btn.config(state=DISABLED)
    a()
    c()
    d()
    d()
    msgLabel["text"]="Done"
    msgLabel.pack()
    btn = Button(msg_window, text="Ok", command=msg_window.destroy())
    btn.pack()
    btn.config(state=NORMAL)
    e()
    if tkMessageBox.askyesno( ___ ):
        do something
    else:
        do something
    pass

def c():
    return

def d():
    return

def e():
    return

root = Tk()
frame = Frame( ___ )
frame.pack()
but_find = Button( ___ )
but_find.pack()
but_start = Button( ___ )
but_start.pack()
but_exit = Button( ___ )
but_exit.pack()
root.mainloop()

说明:我正在构建一个应用程序,其中根窗口包含 3 个按钮.当start 按钮被点击时,function b 被调用,它依次调用function b 内部的各种其他函数.这就是我想创建一个显示在根窗口上方的第二个窗口并使根窗口处于非活动状态的点,直到 function b 完成或直到 second window被摧毁了.

Explanation: I am building an application where the root window contains 3 buttons. When start button is clicked, function b is called, which in turn calls various other functions inside function b itself. This is the point where, i want to create a second window that shows up above the root window and making the root window inactive until function b gets completed or until the second window is destroyed.

我想要的是..经过一些步骤后,第二个窗口(包含 2 个东西 - 一条消息和一个按钮)应该在 function b 内开始.它应该在 function a 开始之前显示消息 Processing.... 和一个名为 Okinactive button 并且应该出现直到 function d 完成.然后,我想显示 Done 与破坏第二个窗口的 active 'Ok' button.然后激活 root 窗口.

What i want is.. The second window(contains 2 things- a message and a button) should start inside the function b after some steps. It should display the message Processing.... and an inactive button called Ok before function a starts and should appear until function d gets completed. Then, i want to display Done with an active 'Ok' button that destroys the second window. Then make the root window active.

推荐答案

尝试开发您自己的对话框.示例:

Try developing your own dialog. Example:

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).

And to disable the first button, call self.buttonconfig(1, state, DISABLED) (1 refers to the number of the button). To enable it, use self.buttonconfig(1, state, NORMAL).

这篇关于如何在 Python 中使用 Tkinter 创建自定义消息框并更改消息和更改按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆