[英]Changing text of label does not work
I have a problem with the following python script. 我的以下python脚本有问题。 Later, it will catch the data from a barcode scanner and display the text as a label.
稍后,它将从条形码扫描仪捕获数据并将文本显示为标签。 But whenever the text should be changed from the label (highlighted line), the program crashes.
但是,每当应从标签(突出显示的行)更改文本时,程序就会崩溃。 I am an absolute beginner Python and can not explain that.
我是绝对的Python初学者,无法解释这一点。 I comment out the line, the program works.
我注释掉该行,该程序有效。
from Tkinter import *
import pyHook
class Application(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Sc4nn0r")
self.variable = "Start Variable"
self.master.geometry("363x200")
self.master.resizable(0,0)
self.master.rowconfigure( 0, weight = 1)
self.master.columnconfigure( 0, weight = 1 )
self.grid( sticky = W+E+N+S )
self.label4String = StringVar()
self.label4 = Label(self, textvariable=self.label4String)
self.label4.grid( row = 2, column = 1, columnspan = 2, sticky = W+E+N+S)
self.label4String.set("Variable1")
self.string = ''
hook = pyHook.HookManager()
hook.KeyDown = self.read
hook.HookKeyboard()
def read(self, event):
print(event.Ascii);
if event.Ascii != 13:
self.string = self.string + chr(event.Ascii)
else:
self.post(self.string.strip(' \0'))
self.string = ''
return True
def post(self,string):
self.label4String.set(string) # THIS LINE I Mean ##########
print(string)
def main():
Application().mainloop()
if __name__ == '__main__':
main()
I hope it can someone help me. 我希望有人能帮助我。
I would suggest getting rid of StringVar in its entirety. 我建议完全删除StringVar。 Instead, use
self.label4 = Label(self, text = "Variable1")
. 而是使用
self.label4 = Label(self, text = "Variable1")
。 Then, whenever you wish to change the label, you can use self.label4.config(text = string)
. 然后,每当您希望更改标签时,都可以使用
self.label4.config(text = string)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.