Tkinter – GUI:用户文本输入,带有检查输入异常并关闭窗口的按钮。

huangapple go评论100阅读模式
英文:

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&lt;=0:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;user_input_1 must be a positive number(&gt;0). Please provide a different value!&quot;)
except ValueError:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;Please provide a numeric value for user_input_1!&quot;)
try:
float(user_input_2)
user_input_2=float(user_input_2)
if user_input_2&lt;=0:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;user_input_2 must be a positive number(&gt;0). Please provide a different value!&quot;)
except ValueError:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;Please provide a numeric value for user_input_2!&quot;)
window.destroy()
else:
tkinter.messagebox.showwarning(title=&quot;Error&quot;, message=&quot;Provide values for all entry boxes!&quot;)
#GUI 
window = tkinter.Tk()
window.title(&quot;Dimensions Data Entry&quot;)
window.resizable(0,0)
frame = tkinter.Frame(window)
frame.pack()
def disable_event():
pass
window.protocol(&quot;WM_DELETE_WINDOW&quot;, disable_event)
#DIMENSIONS
dimensions_frame =tkinter.LabelFrame(frame, text=&quot;Dimensions&quot;)
dimensions_frame.grid(row= 0, column=0, padx=20, pady=10)
user_input_1_label = tkinter.Label(dimensions_frame, text=&quot;user_input_1:&quot;)
user_input_1_label.grid(row=0, column=0)
user_input_2_label = tkinter.Label(dimensions_frame, text=&quot;user_input_2:&quot;)
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=&quot;Enter Data&quot;, command=enter_data_close)
button.grid(row=3, column=0, sticky=&quot;news&quot;, 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&lt;=0:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;user_input_1 must be a positive number(&gt;0). Please provide a different value!&quot;)
return
except ValueError:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;Please provide a numeric value for user_input_1!&quot;)
return
try:
float(user_input_2)
user_input_2=float(user_input_2)
if user_input_2&lt;=0:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;user_input_2 must be a positive number(&gt;0). Please provide a different value!&quot;)
return
except ValueError:
tkinter.messagebox.showwarning(title=&quot;Value Error!&quot;, message=&quot;Please provide a numeric value for user_input_2!&quot;)
return
window.destroy()
else:
tkinter.messagebox.showwarning(title=&quot;Error&quot;, message=&quot;Provide values for all entry boxes!&quot;)
return

huangapple
  • 本文由 发表于 2023年8月4日 02:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定