英文:
Tkinter - GUI: user text input with button that checks input for exceptions and closes window
问题
我正在创建一个GUI,该GUI将在文本字段中接受用户输入,然后在按下“输入数据”按钮后,程序将检查用户输入是否为数字(稍后在代码中再次使用输入)。我正在尝试使按钮仅在没有引发异常时关闭窗口(即两个文本字段都包含文本且为数字)。我应该在何处放置“window.destroy()”方法?
当前,无论如何程序都会关闭,并且只会输出异常警告。
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
def enter_data_close():
global user_input_1
global user_input_2
user_input_1 = user_input_1_entry.get()
user_input_2 = user_input_2_entry.get()
if user_input_1 and user_input_2:
try:
float(user_input_1)
user_input_1 = float(user_input_1)
if user_input_1 <= 0:
tkinter.messagebox.showwarning(title="Value Error!", message="user_input_1 must be a positive number (>0). Please provide a different value!")
except ValueError:
tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_1!")
try:
float(user_input_2)
user_input_2 = float(user_input_2)
if user_input_2 <= 0:
tkinter.messagebox.showwarning(title="Value Error!", message="user_input_2 must be a positive number (>0). Please provide a different value!")
except ValueError:
tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_2!")
window.destroy()
else:
tkinter.messagebox.showwarning(title="Error", message="Provide values for all entry boxes!")
# GUI
window = tkinter.Tk()
window.title("Dimensions Data Entry")
window.resizable(0, 0)
frame = tkinter.Frame(window)
frame.pack()
def disable_event():
pass
window.protocol("WM_DELETE_WINDOW", disable_event)
# DIMENSIONS
dimensions_frame = tkinter.LabelFrame(frame, text="Dimensions")
dimensions_frame.grid(row=0, column=0, padx=20, pady=10)
user_input_1_label = tkinter.Label(dimensions_frame, text="user_input_1:")
user_input_1_label.grid(row=0, column=0)
user_input_2_label = tkinter.Label(dimensions_frame, text="user_input_2:")
user_input_2_label.grid(row=0, column=1)
user_input_1_entry = tkinter.Entry(dimensions_frame)
user_input_2_entry = tkinter.Entry(dimensions_frame)
user_input_1_entry.grid(row=1, column=0)
user_input_2_entry.grid(row=1, column=1)
for widget in dimensions_frame.winfo_children():
widget.grid_configure(padx=10, pady=5)
# ENTER DATA AND CLOSE BUTTON
button = tkinter.Button(frame, text="Enter Data", command=enter_data_close)
button.grid(row=3, column=0, sticky="news", padx=20, pady=10)
window.mainloop()
英文:
I am creating a GUI that will accept user input in the text fields, and then after pressing the "Enter Data" button the program will check if the user input is a numeric (the input is later used later down the code). I am trying to make the button only close the window if no exception is raised (i.e. both text fields have text and it's numeric). Where should I place the "window.destroy()" method?
Right now the program closes no matter what, and just spits out the exception warnings.
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
def enter_data_close():
global user_input_1
global user_input_2
user_input_1=user_input_1_entry.get()
user_input_2=user_input_2_entry.get()
if user_input_1 and user_input_2:
try:
float(user_input_1)
user_input_1=float(user_input_1)
if user_input_1<=0:
tkinter.messagebox.showwarning(title="Value Error!", message="user_input_1 must be a positive number(>0). Please provide a different value!")
except ValueError:
tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_1!")
try:
float(user_input_2)
user_input_2=float(user_input_2)
if user_input_2<=0:
tkinter.messagebox.showwarning(title="Value Error!", message="user_input_2 must be a positive number(>0). Please provide a different value!")
except ValueError:
tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_2!")
window.destroy()
else:
tkinter.messagebox.showwarning(title="Error", message="Provide values for all entry boxes!")
#GUI
window = tkinter.Tk()
window.title("Dimensions Data Entry")
window.resizable(0,0)
frame = tkinter.Frame(window)
frame.pack()
def disable_event():
pass
window.protocol("WM_DELETE_WINDOW", disable_event)
#DIMENSIONS
dimensions_frame =tkinter.LabelFrame(frame, text="Dimensions")
dimensions_frame.grid(row= 0, column=0, padx=20, pady=10)
user_input_1_label = tkinter.Label(dimensions_frame, text="user_input_1:")
user_input_1_label.grid(row=0, column=0)
user_input_2_label = tkinter.Label(dimensions_frame, text="user_input_2:")
user_input_2_label.grid(row=0, column=1)
user_input_1_entry = tkinter.Entry(dimensions_frame)
user_input_2_entry = tkinter.Entry(dimensions_frame)
user_input_1_entry.grid(row=1, column=0)
user_input_2_entry.grid(row=1, column=1)
for widget in dimensions_frame.winfo_children():
widget.grid_configure(padx=10, pady=5)
# ENTER DATA AND CLOSE BUTTON
button = tkinter.Button(frame, text="Enter Data", command=enter_data_close)
button.grid(row=3, column=0, sticky="news", padx=20, pady=10)
window.mainloop()
答案1
得分: 1
你已经非常接近了。只需在你的消息框代码末尾添加return
。
def enter_data_close():
global user_input_1
global user_input_2
user_input_1 = user_input_1_entry.get()
user_input_2 = user_input_2_entry.get()
if user_input_1 and user_input_2:
try:
float(user_input_1)
user_input_1 = float(user_input_1)
if user_input_1 <= 0:
tkinter.messagebox.showwarning(title="数值错误!", message="user_input_1 必须是正数(>0)。请提供不同的值!")
return
except ValueError:
tkinter.messagebox.showwarning(title="数值错误!", message="请为 user_input_1 提供数值!")
return
try:
float(user_input_2)
user_input_2 = float(user_input_2)
if user_input_2 <= 0:
tkinter.messagebox.showwarning(title="数值错误!", message="user_input_2 必须是正数(>0)。请提供不同的值!")
return
except ValueError:
tkinter.messagebox.showwarning(title="数值错误!", message="请为 user_input_2 提供数值!")
return
window.destroy()
else:
tkinter.messagebox.showwarning(title="错误", message="为所有输入框提供值!")
return
英文:
You're really close. Just add return
to the end of your messageboxes.
def enter_data_close():
global user_input_1
global user_input_2
user_input_1=user_input_1_entry.get()
user_input_2=user_input_2_entry.get()
if user_input_1 and user_input_2:
try:
float(user_input_1)
user_input_1=float(user_input_1)
if user_input_1<=0:
tkinter.messagebox.showwarning(title="Value Error!", message="user_input_1 must be a positive number(>0). Please provide a different value!")
return
except ValueError:
tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_1!")
return
try:
float(user_input_2)
user_input_2=float(user_input_2)
if user_input_2<=0:
tkinter.messagebox.showwarning(title="Value Error!", message="user_input_2 must be a positive number(>0). Please provide a different value!")
return
except ValueError:
tkinter.messagebox.showwarning(title="Value Error!", message="Please provide a numeric value for user_input_2!")
return
window.destroy()
else:
tkinter.messagebox.showwarning(title="Error", message="Provide values for all entry boxes!")
return
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论