[英]Why is the function being seen as a global variable
我收到的错误是:未定义全局名称'OnButtonClick2'。 我不确定程序是否将函数作为变量而不是作为函数读取,并且我不知道该怎么做才能对其进行修复。 我同时通过了两个参数,所以我对为什么它不起作用感到困惑。
import Tkinter
def convertDtoB(binary,left):
if(left>0):
binary+=str(left%2)
return convertDtoB(binary,left//2)
else:
binary = int(binary[::-1])
return binary
def convertBtoD(decimal):
answer = 0
length = len(str(decimal))
decimal2 = str(decimal)[::-1]
for i in range(length):
answer+=int(decimal2[i])*2**i
return answer
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.button1 = Tkinter.Button(self, text="Convert Decimal to Binary", command=lambda: self.OnButtonClick(1))
self.button1.grid(column=1,row=1)
self.button2 = Tkinter.Button(self, text="Convert Binary to Decimal", command=lambda: self.OnButtonClick(2))
self.button2.grid(column=1,row=2)
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label.grid(column=1,row=0,columnspan=2,sticky='')
self.labelVariable.set("Select a conversion.")
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
def OnButtonClick2(self, button_id):
if button_id == 3:
binaryNumber = convertDtoB("",self.decimalNumber)
self.labelVariable = Tkinter.StringVar()
label_2 = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label_2.grid(column=1,row=0,columnspan=2,sticky='')
self.label_2.set(binaryNumber)
elif button_id == 4:
binaryNumber = convertBtoD("",self.binaryNumber)
self.labelVariable = Tkinter.StringVar()
label_2 = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label_2.grid(column=1,row=0,columnspan=2,sticky='')
self.label_2.set(binaryNumber)
def OnButtonClick(self, button_id):
if button_id == 1:
self.button1.destroy()
self.button2.destroy()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.decimalNumber = self.entryVariable.set("Enter Decimal Number Here")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
button3 = Tkinter.Button(self, text="Click here to convert", command=lambda: self.OnButtonClick(3))
button3.grid(column=1,row=1)
self.OnButtonClick2(3)
elif button_id == 2:
self.button1.destroy()
self.button2.destroy()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.binaryNumber = self.entryVariable.set("Enter Binary Number Here.")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
button4 = Tkinter.Button(self, text="Click here to convert", command=lambda: self.OnButtonClick(4))
button4.grid(column=1,row=1)
self.OnButtonClick2(4)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()
如果在一个类中定义了这些函数,那么您对self
使用很可能是不正确的,因此它们是该类的 方法/成员函数 。 如果是这种情况:
您应该像这样调用方法:
self.OnButtonClick2(3)
假设这些函数是在类中定义的方法(否则您对self
的使用是不正确的),那么这是在其自己的类中调用已定义方法的正确方法。
课堂信息在这里
通过使用self
我假设您在课堂上。 要调用您要使用的其他类方法:
self.OnButtonClick2(3)
问题中的代码存在一些语法问题。
这些是函数还是类中的方法? 我问,因为第一个参数是“自我”。 使我相信这些是类中的方法。 在这种情况下,您可以执行以下方法:
self.OnButtonClick2(3)
OnButtonClick2
定义为类的方法:
class simpleapp_tk(Tkinter.Tk):
...
def OnButtonClick2(self, button_id):
在执行此操作时,必须通过给该函数加上基于此类的对象实例的前缀来调用此函数。 例如,要从类外部调用它,您将执行以下操作:
app = simpleapp_tk()
...
app.OnButtonClick2(...)
如果要从类中调用或引用此函数,则代码必须引用本身,按照惯例,该自身是self
。 因此,要从类中调用该函数,您需要执行以下操作:
self.OnButtonClick(...)
在问题的原始版本中,代码中有一个地方会忽略self.
部分:
OnButtonClick2(self,3)
由于OnButtonCllick2
仅存在于类内部,并且由于您没有告诉python您要使用哪个类实例来调用此函数,因此python假定您正在尝试调用全局函数。 由于该名称不存在全局函数,因此会给出您报告的错误。
解决方案应该很清楚:当您调用OnButtonClick
,从类内部进行调用时始终使用self.OnButtonClick
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.