How to use Python tkSimpleDialog.askstring?


In Python, the Tkinter library provides a robust set of tools for building GUIs, and "tkSimpleDialog.askstring" is one such tool that facilitates user input through a simple dialog.

Understanding Tkinter and Simple Dialogs

Tkinter is a standard GUI toolkit for Python that allows developers to create desktop applications with ease. It provides a variety of widgets, including buttons, labels, and entry fields, to build interactive interfaces. Tkinter's tkSimpleDialog module specifically focuses on providing simple dialogs for user interaction, and askstring is one of the functions it offers.

Getting Started with tkSimpleDialog.askstring

The askstring function is designed to prompt the user for a string input through a dialog. Let's start by creating a basic Tkinter window and using askstring to get user input −

Example

import tkinter as tk
from tkinter import simpledialog

# Create the main Tkinter window
root = tk.Tk()
root.title("tkSimpleDialog.askstring Example")
root.geometry("720x250")

# Function to show the string input dialog
def get_string_input():
   result = simpledialog.askstring("Input", "Enter a string:")
   if result:
      # Do something with the entered string 
      print("Entered string:", result)

# Create a button that, when clicked, calls the get_string_input function
button = tk.Button(root, text="Get String Input", command=get_string_input)
button.pack(pady=20)

# Start the Tkinter event loop
root.mainloop()

Output

This code sets up a basic Tkinter window with a button. When the button is clicked, the get_string_input function is called, which in turn uses askstring to display a dialog with the specified prompt ("Enter a string:"). The entered string is then printed to the console.

Customizing the Dialog

The askstring function allows you to customize various aspects of the dialog, such as the title, prompt, and initial value. Let's explore how to customize these parameters −

Example

import tkinter as tk
from tkinter import simpledialog

# Create the main Tkinter window
root = tk.Tk()
root.title("Customizing tkSimpleDialog.askstring")
root.geometry("720x250")

# Function to show the customized string input dialog
def get_custom_string_input():
   result = simpledialog.askstring(
      "Custom Input", "Enter your name:",
      initialvalue="TutorialsPoint.com"
   )
   if result:
      print("Entered name:", result)

# Create a button to call the get_custom_string_input function
button = tk.Button(
   root, text="Get Custom String Input",
   command=get_custom_string_input
)
button.pack(pady=20)

# Start the Tkinter event loop
root.mainloop()

In this example, the askstring function is customized with a title ("Custom Input"), a prompt ("Enter your name:"), and an initial value ("John Doe"). These customizations make the dialog more specific to the application's context.

Output

On running this code, you will get the following output window −

After providing the custom string input, you can have it printed on the console −

Entered name: Sandeep Sahu

Practical Applications of tkSimpleDialog.askstring

Now that we have a basic understanding of how to use askstring, let's explore some practical applications of this function.

Gathering user credentials

Take a look at the following example −

Example

import tkinter as tk
from tkinter import simpledialog

# Create the main Tkinter window
root = tk.Tk()
root.title("User Credentials Example")
root.geometry("720x250")

# Function to get username and password
def get_credentials():
   username = simpledialog.askstring(
      "Login", 
      "Enter your username:"
   )
   if username:
      password = simpledialog.askstring(
         "Login", "Enter your password:", 
         show="*"
      )
      if password:
         # Do something with the entered username and password
         print("Username:", username)
         print("Password:", password)

# Create a button to call the get_credentials function
button = tk.Button(root, text="Login", command=get_credentials)
button.pack(pady=20)

# Start the Tkinter event loop
root.mainloop()

In this example, the askstring function is used to collect both the username and password. The show="*" argument is used to hide the entered characters for the password, enhancing security.

Output

On running this code, you will get the following output window −

After modifying the credentials, you can have the revised details printed on the console −

Username: Arun Kumar
Password: HelloArun

Receiving User Preferences or Settings

Take a look at the following example −

Example

import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()
root.title("User Preferences Example")
root.geometry("720x250")

# Function to get user preferences
def get_preferences():
   # Get user's favorite color
   color = simpledialog.askstring(
      "Preferences", "Enter your favorite color:",
      initialvalue="Blue"
   )
   if color:
      # Get user's preferred font size
      font_size = simpledialog.askstring(
         "Preferences", "Enter your preferred font size:",
         initialvalue="12"
      )
      if font_size:
         # Do something with the entered preferences
         print("Favorite Color:", color)
         print("Preferred Font Size:", font_size)

# Create a button that, when clicked, calls the get_preferences function
button = tk.Button(root, text="Set Preferences", command=get_preferences)
button.pack(pady=20)

root.mainloop()

This example demonstrates how askstring can be used to collect user preferences, such as their favorite color and preferred font size.

Output

On running this code, you will get the following output window −

After setting the preferences, you will have the revised settings printed on the console −

Favorite Color: Blue
Preferred Font Size: 20

Conclusion

In this tutorial, we explored the usage of "tkSimpleDialog.askstring" in Python's Tkinter. We started with the basics, understanding how to create a simple string input dialog and gradually moved into customization options.

By providing practical examples, we showcased the versatility of askstring in collecting various types of user input, from simple strings to sensitive information like passwords.

Updated on: 15-Feb-2024

4 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements