Tk GUI 一直有效,直到我尝试添加一个复选框
Tk GUI works until I try to add a check box
我有以下代码可以正常工作...
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'
的帖子,但我越来越困惑。
为什么添加复选框会使程序崩溃?
App
不是一个小部件,所以在 class self
里面不能用作另一个小部件的主人,这就是你对 [=13 所做的=].
在您的情况下,您正在创建一个名为 frame
的框架,您可以将其用作主框架。
self.chk = Checkbutton(frame, text="chain", variable=self.chained)
我有以下代码可以正常工作...
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'
的帖子,但我越来越困惑。
为什么添加复选框会使程序崩溃?
App
不是一个小部件,所以在 class self
里面不能用作另一个小部件的主人,这就是你对 [=13 所做的=].
在您的情况下,您正在创建一个名为 frame
的框架,您可以将其用作主框架。
self.chk = Checkbutton(frame, text="chain", variable=self.chained)