tkinter:调用消息框后无法输入 [英] tkinter: No input possible after messagebox is called
问题描述
我想在程序开始时调用一个消息框(带有messagebox.showwarning),以通知用户某些信息.当用户点击确定"时,GUI打开.用户可以在那里输入.
I want to call a messagebox (with messagebox.showwarning) at the start of my program to inform the user about something. When the user clicks on "Ok", a GUI opens. There the user can make inputs.
问题:显示警告后无法输入.没有警告窗口,输入即可正常工作.
Problem: No input is possible after the warning is displayed. Without the warning window the input works without any problems.
用户可以提供输入解决方案吗?
Is there a solution that the user can provide input?
我在此处包括了一个代码段:
I have included a code snippet here:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
root = tk.Tk()
root.geometry('420x100')
# Input doesn't work after the messagebox
messagebox.showwarning("Warning", "Some Serious Warning.")
# Label
li = Label(root, text="Input 1")
li.grid(row=0, column=0)
li = Label(root, text="Input 2")
li.grid(row=0, column=2)
li = Label(root, text="Dropdown")
li.grid(row=1, column=0)
li = Label(root, text="Input 3")
li.grid(row=1, column=2)
# Input
url_text = StringVar()
e1 = Entry(root, textvariable=url_text)
e1.grid(row=0, column=1)
# Dropdown Menu
OptionList = [
"1",
"2",
"3"
]
input1 = StringVar()
input1.set(OptionList[0])
e3 = tk.OptionMenu(root, input1, *OptionList)
e3.config(width=12, font=('Helvetica', 9))
e3.grid(row=1, column=1)
input2 = StringVar()
e2 = Entry(root, textvariable=input2)
e2.grid(row=0, column=3)
input3 = StringVar()
e4 = Entry(root, textvariable=input3)
e4.grid(row=1, column=3)
# Button
b1 = Button(root, text="Run", width=12)
b1.grid(row=2, column=1)
b2 = Button(root, text="Help", width=12)
b2.grid(row=2, column=2)
b3 = Button(root, text="Exit", width=12)
b3.grid(row=2, column=3, pady=(10, 10))
root.mainloop()
推荐答案
在显示消息之前,您需要调用 root.wait_visibility()
或 root.update()
框:
You need to call root.wait_visibility()
or root.update()
before showing the message box:
root.wait_visibility() # or root.update()
messagebox.showwarning("Warning", "Some Serious Warning.")
或在显示消息框后使用 foucs_force()
获得焦点:
Or get the focus using foucs_force()
after showing the message box:
messagebox.showwarning("Warning", "Some Serious Warning.")
root.focus_force()
这篇关于tkinter:调用消息框后无法输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!