0

我在顶级小部件中使用 simpledialog 小部件时遇到问题。下面的代码摘录会导致一个空的弹出窗口(标题为“Blocked fields”)、带有正确简单对话框的第二个弹出窗口(也可以正常工作)和主游戏窗口(代码中未显示)。

我想摆脱第二个过时的窗口,我认为它一定是一件简单的事情,但我被卡住了(完整的 python 新手,如果你还不能说的话)。任何提示高度赞赏!

import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()
root.geometry("580x400+300+200")
root.title("Pah Tum")

#Popup window
block_request_top = tk.Toplevel()
block_request_top.title("Blocked fields")
entry_block = simpledialog.askinteger("Blocked fields", \
"Please enter a number of fields to be blocked. Choose an \
uneven number between 5,13]", parent=block_request_top, minvalue=5, \
maxvalue=13)
4

2 回答 2

1

你根本不需要Toplevel()窗户。askinteger()是一个对话框,不需要容器小部件。只需跳过 block_request_top 窗口代码。

import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()
root.geometry("580x400+300+200")
root.title("Pah Tum")

# Popup window
#block_request_top = tk.Toplevel()
#block_request_top.title("Blocked fields")
entry_block = simpledialog.askinteger("Blocked fields",
"Please enter a number of fields to be blocked. Choose an \
uneven number between 5,13]", parent=root, minvalue=5, # parent changed...
maxvalue=13)
print('Okay, I will block %d fields.' % entry_block) # new, to see value
# set up the rest of your GUI
root.mainloop() # You need this for the GUI to remain alive.

parent 的值已更新为 root,以反映 Toplevel 窗口正在消失。

最后还需要调用 root.mainloop() 以保持 GUI 处于活动状态和运行状态。一旦你的程序到达这里,Tkinter 系统基本上只是等待“事件”发生,比如用户单击按钮或输入字段。您仍然必须将所有这些与您必须绘制的所有按钮联系在一起。有几个人发布了同样的问题。

于 2017-12-15T16:59:33.953 回答
0

针对这个特定问题实现这一点的最简单方法可能是使用withdraw,iconifydeiconify方法,方法是在它们之间创建entry_block,如下所示:

...
block_request_top.withdraw()

entry_block = simpledialog.askinteger("Blocked fields", \
"Please enter a number of fields to be blocked. Choose an \
uneven number between 5,13]", parent=block_request_top, minvalue=5, \
maxvalue=13)

block_request_top.iconify()
block_request_top.deiconify()

整个代码:

import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()
root.geometry("580x400+300+200")
root.title("Pah Tum")

#Popup window
block_request_top = tk.Toplevel()
block_request_top.title("Blocked fields")

block_request_top.withdraw()

entry_block = simpledialog.askinteger("Blocked fields", \
"Please enter a number of fields to be blocked. Choose an \
uneven number between 5,13]", parent=block_request_top, minvalue=5, \
maxvalue=13)

block_request_top.iconify()
block_request_top.deiconify()

我非常怀疑这是你最终会使用的东西。

于 2017-12-15T14:21:46.270 回答