gpt4 book ai didi

python - 默认情况下不检查 Tkinter 单选按钮

转载 作者:行者123 更新时间:2023-12-01 01:27:58 26 4
gpt4 key购买 nike

我很茫然。默认情况下,我的单选按钮不会检查,即使我遵循了各种单选按钮教程。不确定我做错了什么,我将默认单选按钮设置为 False 按钮​​。

这是我的代码:

import tkinter as tk
import tkinter.ttk as ttk
from tkinter.colorchooser import *


class ExampleView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)

""" create StringVars for our shape fill radio button group """
shape_fill_status = tk.StringVar()

""" create radio button group for turning off shape fill """
shape_fill_frame = tk.Frame(self)
shape_fill_label = tk.Label(shape_fill_frame, text="Fill Shape: ", anchor="w")
shape_fill_label.pack(side=tk.LEFT, padx = 10)
shape_fill_false = tk.Radiobutton(shape_fill_frame, text = "False", variable = shape_fill_status, value = 0)

shape_fill_true = tk.Radiobutton(shape_fill_frame, text = "True", variable = shape_fill_status, value = 1)
shape_fill_status.set(0)
shape_fill_false.pack(side=tk.LEFT, padx = 10)
shape_fill_true.pack(side=tk.LEFT, padx = 10)
shape_fill_frame.grid(row=0, column=0, stick="w")

self.grid_columnconfigure(0, weight=1)

if __name__ == "__main__":
root = tk.Tk()
view = ExampleView(root)
view.pack(side="top", fill="both", expand=True)
root.wm_geometry("400x200")
root.mainloop()

这是我的 tkinter 小部件的样子:

screenshot of widget

最佳答案

您正在使用局部变量来存储StringVar。一旦 __init__ 完成,变量就会被销毁。

将所有地方的 shape_fill_status 更改为 self.shape_fill_status

关于python - 默认情况下不检查 Tkinter 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53181118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com