我正在嘗試使用Python Tkinter創建應用程序。在第47行的這個程序中,我使用了布爾語句。但它給了我一個錯誤。我想使用布爾語句作爲內存位,並希望在程序中的任何地方使用。在Python中的布爾語句中給出錯誤
import serial
import Tkinter
from Tkinter import *
class SerialViewer:
def __init__(self):
self.win = Tk()
self.ser = serial.Serial('com8',9600)
def bt1 (self):
self.ser.write('on')
def bt2 (self):
self.ser.write('off')
def bt3 (self):
self.ser.write(self.v.get())
def makewindow (self):
frame1 = Frame(self.win)
frame1.pack(side = LEFT)
b1 = Button(frame1, text = "ON", command = self.bt1)
b2 = Button(frame1, text = "OFF", command = self.bt2)
b1.grid(row = 0, column = 0)
b2.grid(row = 0, column = 1)
frame2 = Frame(self.win)
frame2.pack()
self.v = StringVar()
r1 = Radiobutton(frame2,text = 'on', variable = self.v, value = 'on')
r2 = Radiobutton(frame2,text = 'off', variable = self.v, value = 'off')
r1.select()
b3 = Button(frame2, text = 'send', command = self.bt3)
b3.pack(sid = RIGHT, padx = 5)
r1.pack(side = LEFT)
r2.pack(side = LEFT)
frame3 = Frame(self.win)
frame3.pack()
self.d = StringVar()
self.d.set('default')
label = Label(frame3, textvariable = self.d, relief = RAISED)
label.pack(side = RIGHT)
def update(self):
data = self.ser.readline(self.ser.inWaiting())
self.d.set(data)
if data == 'f1':
self.M1 = True
if self.M1:
print("ok tested")
self.win.after(100,self.update)
def run(self):
self.makewindow()
self.update()
self.win.mainloop()
SerialViewer().run()
The error is:Traceback (most recent call last): File "Untitled", line 58 SerialViewer().run() File "Untitled", line 55, in run self.update() File "Untitled", line 49, in update if self.M1: AttributeError: SerialViewer instance has no attribute 'M1'
非常感謝你 –
沒問題的人!我很樂意提供幫助 –