Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

python - Tkinter: Put simpledialog.askinteger in a toplevel box

I have trouble using the simpledialog widget within a toplevel widget. The code extract below results in an empty pop-up window (entitled "Blocked fields"), a second pop-up window with the correct simpledialog (also working fine) and the main game window (not featured here in the code).

I want to get rid of the second obsolete window, and I reckon it must be a simple thing, but I am stuck (complete python newbie, if you can't tell already). Any hints highly appreciated!

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You shouldn't need the Toplevel() window at all. askinteger() is a dialogbox and does not require a container widget. Just skip the block_request_top window code.

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.

The value of parent was updated to root, to reflect the Toplevel window going away.

You also need the root.mainloop() call at the end, to keep the GUI active and running. Once your program gets here, the Tkinter system essentially just waits for "events" to happen, like the user clicking a button or typing into a field. You still have to tie all this together with all the buttons you have to draw. There are a few people posting about this same problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...