Error with TKinter Attribute Simpledialog

Question:

I’m currently trying to write in a few basic user input boxes using the tkinter module in Python 3.6 (via Spyder). I can confirm that the module loads, and that the option to select simpledialog comes up, but I keep getting the following error:

AttributeError: module ‘tkinter’ has no attribute ‘simpledialog’

Image of tkinter simpledialog

I’ve tried to look for other options, but other user input options do not seem to work on my Python interface. It either crashes, or the data isn’t structured properly.

Interestingly enough, in the past, I’ve done similar things in Python with no errors, but this keeps coming up with this particular programming piece.

import tkinter as tk
import pyodbc as py
py.pooling = False

## INPUT YOUR USER ID AND PASSWORD AND DECLARE YOUR CONNECTION
## THE DIALOG BOXES MAY POP OPEN ON ANOTHER SCREEN
## THE PASSWORD INPUT IS MASKED AND WILL NOT SHOW IN THE
## VARIABLE EXPLORER

ID = tk.simpledialog.askstring("Please input your username.","Username: ")
PW = tk.simpledialog.askstring("Please input your password.",
                               "Password: ", show='*')
CONN = tk.simpledialog.askstring("Please input your connection.",
                                 "Connection: ")

My expected results are that a popup window will appear and that I’ll be able to get the user information I need to maintain a stable connection to the server I’m using.

Thank you in advance for your advice!

Answers:

simpledialog is not in tkinter but in tkinter.simpledialog and you have to import it

import tkinter as tk
import tkinter.simpledialog

root = tk.Tk() # create main window
#root.iconify() # minimize main window 
root.withdraw() # hide main window 

answer = tkinter.simpledialog.askstring("Question", 'Your name:')
print(answer)

#root.destroy()  # should work without it
#root.mainloop() # should work without it

See tkinter modules

Answered By: furas
import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()

ID = simpledialog.askstring("Please input your username.", "Username: ", parent=root)

root.mainloop()

This will keep the popup within the parent window and visible.

Answered By: Andy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.