mercredi 21 juillet 2021

Tk GUI works until I try to add a check box

I have the following code which works fine...

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()

However when I uncomment the check box code

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

I get the following error,

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'

I have looked through posts about AttributeError: 'App' object has no attribute 'tk' but am just getting more & more confused.

Why would adding a checkbox crash the program?




Aucun commentaire:

Enregistrer un commentaire