make a gui that has textbox and a button named done the text box only need numbers cuz…
- User
make a gui that has textbox and a button named done the text box only need numbers cuz it changes my speed walk
- Assistant
make a gui that has textbox and a button named done the text box only need numbers cuz it changes my speed walk
To create a GUI with a textbox that only accepts numbers and a button named "Done" in Python, you can use the tkinter library. Below is a simple example that demonstrates how to create such a GUI. This program will allow the user to enter a number in the textbox to change a variable walk_speed when the "Done" button is pressed. The validate feature of tkinter is used to ensure that only numbers are entered in the textbox.
First, ensure you have Python installed on your system. tkinter is included with the standard library, so you don't need any additional installations.
Here's the code:
import tkinter as tk
from tkinter import messagebox
class SpeedWalkApp:
def __init__(self, root):
self.root = root
self.root.title("Speed Walk Controller")
# Default speed
self.walk_speed = 0
# Instruction label
self.instruction_label = tk.Label(root, text="Enter walking speed:")
self.instruction_label.pack(pady=5)
# Validate the input to ensure only numbers are accepted
vcmd = (root.register(self.validate_entry), '%P')
# Textbox accepting only numbers
self.speed_entry = tk.Entry(root, validate='key', validatecommand=vcmd)
self.speed_entry.pack(pady=5)
# Done button
self.done_button = tk.Button(root, text="Done", command=self.set_speed)
self.done_button.pack(pady=5)
def validate_entry(self, new_value):
# Allow empty string or numbers only
if new_value == "" or new_value.isdigit():
return True
else:
return False
def set_speed(self):
# Set the walking speed from the textbox value
try:
speed = int(self.speed_entry.get())
self.walk_speed = speed
messagebox.showinfo("Success", f"Walking speed set to {self.walk_speed}")
except ValueError:
messagebox.showerror("Error", "Invalid number entered.")
if __name__ == '__main__':
root = tk.Tk()
app = SpeedWalkApp(root)
root.mainloop()
Explanation of the code:
tkinter library is imported to create the GUI.SpeedWalkApp is created to encapsulate the GUI logic.__init__): Initializes the main window, label, textbox, and button.validate_entry Method: Ensures that the textbox only accepts numeric input.set_speed Method: Called when the "Done" button is pressed. It retrieves the value from the textbox, sets the walk_speed, and shows a confirmation message.Copy and paste this code into a Python file and run it. The resulting window will have a textbox for numeric input and a "Done" button to set the walking speed.
Not yet included in a published release.