Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

Easiest way to achieve this for this specific problem may be making use of withdraw, iconify and deiconify methods by creating entry_block in between them as in:

...
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()

entire code:

import tkinter as tk
from tkinter import simpledialog

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

#Popupwindow
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()

I highly doubt that this is what you will eventually end up using though.

Post a Comment for "Tkinter: Put Simpledialog.askinteger In A Toplevel Box"