Tkinter – GUI:带有按钮的复选框,用于检查异常并关闭窗口

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

Tkinter - GUI: checkbox with button that checks for exceptions and closes window

问题

我正在创建一个GUI,它将向用户显示一个带有复选框的选项,并在按下“输入数据”按钮后检查用户是否至少选中了一个复选框。选中的值将被分配给一个字典,在代码的后面将使用该字典。我试图使按钮只在没有引发异常时关闭窗口(即至少选中一个复选框)。

目前,无论用户是否选中了至少一个复选框,程序都会显示异常消息。

import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def enter_data():
    global option_vars

    try:
        if any(option_vars):
            raise Exception("请至少选择一个复选框!")
            return
    except Exception as ex:
        tkinter.messagebox.showwarning(title="选择错误!", message=ex)
        return

    window.destroy()

#GUI 

window = tkinter.Tk()
window.title("数据输入")
window.resizable(0,0)

frame = tkinter.Frame(window)
frame.pack()

def disable_event():
    pass
window.protocol("WM_DELETE_WINDOW", disable_event)

#疲劳情况

option_type_frame =tkinter.LabelFrame(frame, text="类型")
option_type_frame.grid(row= 0, column=0, padx=20, pady=10)

option_type_option=["类型1", "类型2", "类型3", "类型4"]

option_vars = {}
for option_type in option_type_option:
    var = StringVar(value="")
    option_vars[option_type] = var
    check_button= Checkbutton(
        option_type_frame,
        text=option_type,
        variable=var,
        onvalue=option_type,
        offvalue="",
        padx=20,
        pady=10
    )

for widget in option_type_frame.winfo_children():
    widget.grid_configure(padx=10, pady=5)

# 输入数据按钮
button = tkinter.Button(frame, text="输入数据", command=enter_data)
button.grid(row=2, column=0, sticky="news", padx=20, pady=10)

window.mainloop()
英文:

I am creating a GUI that will display the option to the user with a checkbox, and then after pressing the "Enter Data" button the program will check if the user has pressed at least one of the check-boxes. The checked values are being assigned to a dictionary which will be used later down the code. I am trying to make the button only close the window if no exception is raised (i.e. at least one check box is checked).

Right now the program displays the exception message no matter what, even if the user has checked at least 1 checkbox.

import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def enter_data():
    global option_vars

    try:
        if any(option_vars):
            raise Exception("Please select at least one (1) of the given checkboxes!")
            return
    except Exception as ex:
        tkinter.messagebox.showwarning(title="Selection Error!", message=ex)
        return

    window.destroy()

#GUI 

window = tkinter.Tk()
window.title("Data Entry")
window.resizable(0,0)

frame = tkinter.Frame(window)
frame.pack()

def disable_event():
    pass
window.protocol("WM_DELETE_WINDOW", disable_event)

#FATIGUE CASE

option_type_frame =tkinter.LabelFrame(frame, text="Type")
option_type_frame.grid(row= 0, column=0, padx=20, pady=10)

option_type_option=["Type 1", "Type 2", "Type 3", "Type 4"]

option_vars = {}
for option_type in option_type_option:
    var = StringVar(value="")
    option_vars[option_type] = var
    check_button= Checkbutton(
        option_type_frame,
        text=option_type,
        variable=var,
        onvalue=option_type,
        offvalue="",
        padx=20,
        pady=10
    )

for widget in option_type_frame.winfo_children():
    widget.grid_configure(padx=10, pady=5)

# ENTER DATA BUTTON
button = tkinter.Button(frame, text="Enter Data", command=enter_data)
button.grid(row=2, column=0, sticky="news", padx=20, pady=10)

window.mainloop()

答案1

得分: 1

问题是any(option_vars)将始终返回True,因为它检查字典项是否存在于该实例中。

你想要做的是检查没有任何值存在,这进一步复杂化了,因为这些值是在StringVar中,但可以通过迭代字典值,然后从StringVar调用get()来实现:

if not any([i.get() for i in option_vars.values()]):
英文:

The issue is any(option_vars) will always return True as it is checking that the dictionary items exist in that instance.

What you intend to do is check if none of the values exist, this is further complicated because the values are in a StringVar, but can be accomplished by iterating the dictionary values, then calling a get() from the StringVar:

if not any([i.get() for i in option_vars.values()]):

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

发表评论

匿名网友

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

确定