Tkinter 中的非阻塞信息对话框 [英] non blocking info dialog in Tkinter

查看:32
本文介绍了Tkinter 中的非阻塞信息对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的信息框来显示一些状态输出,我也可以使用 print 将其转储到控制台.我发现的最简单的可能性如下:

I need a simple info box to display some status output, that I would alternatively dump to the console using print. The easiest possibility that I found is the following:

import Tkinter as tk
root = tk.Tk()
root.withdraw()

from tkMessageBox import showinfo    
showinfo('some caption', 'some info')

这个实现的唯一问题是我的程序(不是用 Tkinter 编写的)不会继续运行,直到 showinfo 消息框的确定"按钮被按下.也就是说,showinfo 对话框将被阻止.

The only problem with this implementation is that my program (not written in Tkinter) will not continue running, until the 'ok' button of the showinfo messagebox is pressed. That is, the showinfo dialog will block.

因此我的问题:有没有一种简单的方法可以使 showinfo 非阻塞?Tkinter 中是否有非阻塞的替代消息框实现?我可以想到显示帮助页面的典型用例 - 窗口应该打开并且主程序继续正常运行.

Therefore my question: Is there a simple way to make showinfo non-blocking? Are there alternative messagebox implementations in Tkinter that are non-blocking? I can think of the typical use-cage of showing a help page - the window should open and the main program keeps on running normally.

这是我想出的一个简单的帮助窗口,但不幸的是它没有出现,除非我启动不同的 tkMessageBox 或类似的对象:

here's a simple help window that I came up with, but unfortunately it doesn't show up, unless I launch a different tkMessageBox or a similar object:

class TextInfo(object):

    def __init__(self, parent, window_title = 'window', textfield = 'a text field', label = None):

        self.top = tk.Toplevel(parent)
        self.parent = parent
        self.window_title = window_title
        self.textfield = textfield

        # set window title
        if window_title:
            self.top.title(window_title)

        # add label if given
        if label:
            tk.Label(self.top, text=window_title).grid(row=0)

        # create the text field
        self.textField = tk.Text(self.top, width=80, height=20, wrap=tk.NONE)
        if textfield:
            self.textField.insert(1.0, textfield)
        self.textField.grid(row=1)

        # create the ok button
        b = tk.Button(self.top, text="OK", command=self.ok)
        b.grid(row=2)

    def ok(self):
        self.top.destroy()

这就是我调用窗口的方式:

And this is how I call the window:

root = tk.Tk()
root.withdraw()
TextInfo(self.root, window_title, textfield, label)  
# don't call root.mainloop() here, because this will lead to blocking.

是否需要为窗口设置某种属性或事件?如果我调用 root.mainloop() 窗口会出现,但是我的 GUI 再次被阻塞.

Is there some kind of property or event that I need to set for the window to show up? If I call root.mainloop() the window will show up, but then my GUI is blocked again.

推荐答案

不要使用 tkMessageBox,因为它不允许进行太多配置.只需创建您自己的自定义对话框,看起来像一个.这个页面谈到了很多关于创建自定义 Tkinter 对话框的内容.

Don't use the tkMessageBox as it doesn't allow much configuration. Just create your own custom dialog that looks like one. This page talks a lot about creating custom Tkinter dialogs.

这篇关于Tkinter 中的非阻塞信息对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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