【问题标题】:Tk GUI works until I try to add a check boxTk GUI 一直有效,直到我尝试添加一个复选框
【发布时间】:2021-07-21 21:10:33
【问题描述】:

我有以下代码可以正常工作...

from tkinter import *
import serial
import struct

usbport    = 'COM3'
ser        = serial.Serial(usbport, 9600, timeout=1)
slider_max = 255;
midway     = int(slider_max / 2)

def init():
    print("Started")

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()
        self.scale_0 = Scale(master, from_=0, to=slider_max, command=lambda ev: self.getPWM(0), bd=5, bigincrement=2, length=360, width=30, label='RED')
        self.scale_0.set(midway)
        self.scale_0.pack(side=LEFT)
        self.scale_1 = Scale(master, from_=0, to=slider_max, command=lambda ev: self.getPWM(1), bd=5, bigincrement=2, length=360, width=30, label='GREEN')
        self.scale_1.set(midway)
        self.scale_1.pack(side=LEFT)

        self.centre = Button(frame, text="Centre All", command=self.centre)
        self.centre.pack(side=TOP)
        self.zero = Button(frame, text="Zero All", command=self.zero)
        self.zero.pack(side=LEFT)
        self.maximum = Button(frame, text="Max All", command=self.maximum)
        self.maximum.pack(side=RIGHT)

        # self.chained = IntVar()
        # self.chk = Checkbutton(self, text="chain", variable=self.chained)
        # self.chk.pack(side=BOTTOM)


            
    def getPWM(self, slider):
        
        if slider == 0:
            pwm = self.scale_0.get()
            
        if slider == 1:
            pwm = self.scale_1.get()  
            
        ser.write(struct.pack('>B', 255))
        ser.write(struct.pack('>B', slider))
        ser.write(struct.pack('>B', pwm))
        

    def centre(self):
        
        for idx in range(0, 2):
            
            ser.write(struct.pack('>B', 255))
            ser.write(struct.pack('>B', idx))
            ser.write(struct.pack('>B', midway))
            
        self.scale_0.set(midway)
        self.scale_1.set(midway)


    def zero(self):
        
        for idx in range(0, 2):
            
            ser.write(struct.pack('>B', 255))
            ser.write(struct.pack('>B', idx))
            ser.write(struct.pack('>B', 0))
            
        self.scale_0.set(0)
        self.scale_1.set(0)

        
    def maximum(self):
        
        for idx in range(0, 2):
            
            ser.write(struct.pack('>B', 255))
            ser.write(struct.pack('>B', idx))
            ser.write(struct.pack('>B', slider_max))

            
        self.scale_0.set(slider_max)
        self.scale_1.set(slider_max)


init()
root = Tk()
app = App(root)
root.mainloop()

但是当我取消注释复选框代码时

    self.chained = IntVar()
    self.chk = Checkbutton(self, text="chain", variable=self.chained)
    self.chk.pack(side=BOTTOM)

我收到以下错误,

Traceback (most recent call last):

  File "D:\Tentacle\Servo controller\PC_side_TK_slider_controller.py", line 98, in <module>
    app = App(root)

  File "D:\Tentacle\Servo controller\PC_side_TK_slider_controller.py", line 41, in __init__
    self.chk = Checkbutton(self, text="chain", variable=self.chained)

  File "D:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2646, in __init__
    Widget.__init__(self, master, 'checkbutton', cnf, kw)

  File "D:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2292, in __init__
    BaseWidget._setup(self, master, cnf)

  File "D:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2262, in _setup
    self.tk = master.tk

AttributeError: 'App' object has no attribute 'tk'

我浏览过关于 AttributeError: 'App' object has no attribute 'tk' 的帖子,但我越来越困惑了。

为什么添加复选框会使程序崩溃?

【问题讨论】:

  • 为什么Checkbutton(self, ...) 中有self?不应该是master还是frame
  • @TheLizzard 是的,当然,如果你想把它作为答案,我会给你分数
  • 我对这些点没有帮助:D。我只是来帮助人们的。

标签: python-3.x tkinter checkbox tk


【解决方案1】:

App 不是小部件,因此在该类中 self 不能用作另一个小部件的主控,这就是您对 Checkbutton(self, ... 所做的。

在您的情况下,您正在创建一个名为 frame 的框架,您可以将其用作主框架。

self.chk = Checkbutton(frame, text="chain", variable=self.chained)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多