简体   繁体   English

Tkinter 消息框导致 Entry 禁用

[英]Tkinter Messagebox causing Entry to disable

So I was creating a simple input window with Tkinter but whenever i have a showinfo displaying i can't type in the entry box所以我用 Tkinter 创建了一个简单的输入窗口,但是每当我有一个显示信息时,我都无法在输入框中输入

import tkinter as tk
from tkinter import *
from tkinter.messagebox import *


root = tk.Tk()
root.title("hello world")
root.minsize(700,600)

abc = StringVar()
abc.set("abc")
Entry(root, bd = 1, width = 50, textvariable=abc).pack(side = TOP)
showinfo('info', 'hello')
root.mainloop() 

I'm not sure if there is something wrong with my Python (3.4) or tkinter but whenever i take out the showinfo line I can type into the Entry box but when its there i can't.我不确定我的 Python (3.4) 或 tkinter 是否有问题,但是每当我取出 showinfo 行时,我都可以在输入框中输入,但是当它在那里时我不能。

tkinter messagebox default dialog boxes are modal . tkinter messagebox 默认对话框是模态的 What this means is that you need to close the child window(the tkinter messagebox) before you can return to the parent application.这意味着您需要在返回父应用程序之前关闭子窗口(tkinter 消息框)。

So, there is nothing wrong with your python or tkinter;所以,你的 python 或 tkinter 没有任何问题; This behavior is intended.这种行为是有意的。

Don't show the tkinter messagebox before the event loop is started.在事件循环开始之前不要显示 tkinter 消息框。 Try this:尝试这个:

import tkinter as tk
from tkinter import *
from tkinter.messagebox import *


 def callback():
     showinfo("info", "hello")

 root = tk.Tk()
 root.title("hello world")
 root.minsize(700,600)

 abc = StringVar()
 abc.set("abc")
 Entry(root, bd=1, width=50, textvariable=abc).pack(side=TOP)
 Button(root, text="OK", command=callback).pack()
 root.mainloop()

The solution i made for this is overriding the messagebox.showerror so for example i made我为此所做的解决方案是覆盖 messagebox.showerror 所以例如我做了

import logging
from tkinter import messagebox

                                                           
logging.basicConfig(                                            
    level=logging.DEBUG,                                        
    format='%(asctime)s [%(levelname)s] %(name)s "%(message)s"',
)                                                               
LOGGER = logging.getLogger(__name__)                            

def test_showerror(title, message):              
    LOGGER.debug(f'{title} Message -> {message}')

messagebox.showerror = test_showerror

actually that's how i deal with many problems that face me during writing tests.. i override utility functions to add logging or avoid a case.实际上,这就是我在编写测试时遇到的许多问题的处理方式。我覆盖了实用程序函数以添加日志记录或避免出现案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM