gpt4 book ai didi

python - 为什么函数被视为全局变量

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:53 24 4
gpt4 key购买 nike

我收到的错误是:未定义全局名称“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 是不正确的),那么这是在自己的类中调用已定义方法的正确方法。

类(class)信息 here

关于python - 为什么函数被视为全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21191693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com