python中showinfo_python – Tkinter中的非阻塞信息对话框
我需要一個(gè)簡(jiǎn)單的信息框來(lái)顯示一些狀態(tài)輸出,我可以使用print將其轉(zhuǎn)儲(chǔ)到控制臺(tái).我找到的最簡(jiǎn)單的可能性如下:
import Tkinter as tk
root = tk.Tk()
root.withdraw()
from tkMessageBox import showinfo
showinfo('some caption', 'some info')
這個(gè)實(shí)現(xiàn)的唯一問(wèn)題是我的程序(不是在Tkinter中編寫)將不會(huì)繼續(xù)運(yùn)行,直到按下showinfo消息框的’ok’按鈕.也就是說(shuō),showinfo對(duì)話框?qū)⒈蛔柚?
因此我的問(wèn)題是:是否有一種簡(jiǎn)單的方法可以使showinfo無(wú)阻塞? Tkinter中是否存在非阻塞的備用消息框?qū)崿F(xiàn)?我可以想到顯示幫助頁(yè)面的典型用例 – 窗口應(yīng)該打開(kāi),主程序繼續(xù)正常運(yùn)行.
EDIT1:這是我提出的一個(gè)簡(jiǎn)單的幫助窗口,但不幸的是它沒(méi)有顯示,除非我啟動(dòng)了另一個(gè)tkMessageBox或類似的對(duì)象:
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()
這就是我打電話給窗口的方式:
root = tk.Tk()
root.withdraw()
TextInfo(self.root, window_title, textfield, label)
# don't call root.mainloop() here, because this will lead to blocking.
是否需要為窗口設(shè)置某種屬性或事件?如果我調(diào)用root.mainloop(),窗口將顯示,但然后我的GUI再次被阻止.
最佳答案 不要使用tkMessageBox,因?yàn)樗辉试S太多配置.只需創(chuàng)建一個(gè)看起來(lái)像一個(gè)的自定義對(duì)話框.這篇
page講述了很多關(guān)于創(chuàng)建自定義Tkinter對(duì)話框的內(nèi)容.
總結(jié)
以上是生活随笔為你收集整理的python中showinfo_python – Tkinter中的非阻塞信息对话框的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: multiprocessing.mana
- 下一篇: python装饰器setter_第7.2