在类中使用tkinter checkbutton的新手问题

2024-09-25 08:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

在PyCharm中运行python 3.8

我试图创建一个类来允许创建tkinter checkbutton(以及最终的其他小部件),它将保存小部件的所有格式和定义。我希望能够测试checkbutton的状态,并在回调中对其进行操作。但和许多其他变量一样,我的checkbutton变量似乎总是0。我相当肯定这是一个垃圾收集的问题,但我想不出解决的办法。注意:类定义中的第二组参数指定标签的位置,以显示checkbutton变量状态。目前,回调只是在标签中显示变量状态

确认: 为此,我修改了Burhard Meier的《Python GUI编程食谱》中的代码。与该类相关的代码是我的,其余代码来自Burkhard Meier

'''
This is a modification of code by Burhard A. Meier
in "Python GUI Programming Cookbook"

Created on Apr 30, 2019
@author: Burkhard A. Meier
'''
#======================
# imports
#======================
import tkinter as tk
from tkinter import ttk

# Create instance
win = tk.Tk()

# Add a title
win.title("GUI Test Box")

# Modify adding a Label
a_label = ttk.Label(win, text="Testing TK Widgets")
a_label.grid(column=0, row=0)

# Modified Button Click Function
def click_me():
    action.configure(text='Hello ' + name.get() + ' ' +
                     number_chosen.get())

#Define the Checkbutton Class
class ChkBut(): #lable, xloc, yloc, xlab, ylab
    def __init__(self, lable, xloc, yloc, xlab, ylab):  # , xloc, yloc, text="default", variable = v, onvalue='Set', offvalue='NotSet'):
        v = tk.IntVar()
        self.v = v
        self.lable = lable
        self.xloc = xloc
        self.yloc = yloc
        self.xlab = xlab
        self.ylab = ylab
        c = tk.Checkbutton(
            win,
            text= self.lable,
            variable=self.v,
            command=self.cb(self.v, xlab,ylab))
        c.grid(column=xloc, row=yloc, sticky = tk.W)
        c.deselect()

    def cb (self, c, xlab, ylab):
        if c.get()==1:
            c_label = ttk.Label(win, text="Set")
            c_label.grid(column=xlab, row=ylab)
        elif c.get()==0:
            c_label = ttk.Label(win, text="NotSet")
            c_label.grid(column=xlab, row=ylab)
        else:
            c_label = ttk.Label(win, text=c.v.get())
            c_label.grid(column=xlab, row=ylab)

# Changing the Label
ttk.Label(win, text="Enter a name:").grid(column=0, row=0)

# Adding a Textbox Entry widget
name = tk.StringVar()
name_entered = ttk.Entry(win, width=12, textvariable=name)
name_entered.grid(column=0, row=1)

# Adding a Button
action = ttk.Button(win, text="Click Me!", command=click_me)
action.grid(column=2, row=1)

# Creating three checkbuttons
ttk.Label(win, text="Choose a number:").grid(column=1, row=0)
number = tk.StringVar()
number_chosen = ttk.Combobox(win, width=12, textvariable=number, state='readonly')
number_chosen['values'] = (1, 2, 4, 42, 100)
number_chosen.grid(column=1, row=1)
number_chosen.current(0)

check1 = ChkBut("Button 1", 0, 4, 0, 5)

chVarUn = tk.IntVar()
check2 = tk.Checkbutton(win, text="UnChecked", variable=chVarUn)
check2.deselect()
check2.chVarUn = 0
check2.grid(column=1, row=4, sticky=tk.W)

chVarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text="Enabled", variable=chVarEn)
check3.select()
check3.chVarEn = 0
check3.grid(column=2, row=4, sticky=tk.W)

name_entered.focus()      # Place cursor into name Entry

#======================
# Start GUI
#======================
win.mainloop()

Tags: textnameselfnumbercolumnwinlabeltk