【发布时间】:2015-06-02 06:44:42
【问题描述】:
在 Linux Mint 'Mate' 17 环境中使用 Python 2.7 和 Tkinter
我对 OOP 完全陌生,不明白如何将持久值传递给类实例;在这段代码中,当我在第 20 行和第 22 行使用 Pin_ID 时,会生成“全局未定义”错误:
1 #!/usr/bin/env python
2 import Tkinter as tk
3
4 root = tk.Tk()
5
6 class cbClass:
7 def __init__(self, Pin_ID):
8 self.cb_Txt=tk.StringVar()
9 self.cb_Txt.set("Pin " + Pin_ID + " OFF")
10 self.cb_Var = tk.IntVar()
11 cb = tk.Checkbutton(
12 root,
13 textvariable=self.cb_Txt,
14 variable=self.cb_Var,
15 command=self.cbTest)
16 cb.pack()
17
18 def cbTest(self):
19 if self.cb_Var.get():
20 self.cb_Txt.set("Pin " + Pin_ID + " ON")
21 else:
22 self.cb_Txt.set("Pin " + Pin_ID + " OFF")
23
24 c1 = cbClass("8")
25 c2 = cbClass("E")
26 root.mainloop()
【问题讨论】:
-
我认为,这是因为 Pin_ID,你在
_init_函数中有 Pin_ID 作为参数,但在cbTest中没有