Reputation: 37
The error that I am receiving is: Global name 'OnButtonClick2' is not defined. I am not sure if the program is reading the function as a variable instead of as a function, and I don't know what I should do to fix it. I pass through both the parameters so I'm confused as to why it will not work.
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()
Upvotes: 1
Views: 118
Reputation: 385800
OnButtonClick2
is defined as a method of a class:
class simpleapp_tk(Tkinter.Tk):
...
def OnButtonClick2(self, button_id):
When you do this, you must call this function by prefixing it with the instance of an object based on this class. For example, to call it from outside the class you would do something like this:
app = simpleapp_tk()
...
app.OnButtonClick2(...)
If you want to call or refer to this function from within the class, the code must refer to itself which, by convention, is self
. Thus, to call the function from within the class you would do:
self.OnButtonClick(...)
In the original version of your question, there was one place in your code where you were omitting the self.
part:
OnButtonClick2(self,3)
Since OnButtonCllick2
only lives inside the class, and because you didn't tell python which instance of the class you want to use to call this function, python assumes you are trying to call a global function. Since no global function exists with that name, it gives the error that you report.
The solution should be clear: when you call OnButtonClick
, always use self.OnButtonClick
when calling from inside the class.
Upvotes: 0
Reputation: 7349
Your use of self
is most likely incorrect if these functions are being defined inside a class, and are therefore methods/member functions of that class. If this is the case:
You should be calling the method like so:
self.OnButtonClick2(3)
Assuming these functions are methods being defined inside a class(otherwise your use of self
is incorrect), then this is correct way to call a defined method within it's own class.
Info on classes here
Upvotes: 1
Reputation: 386
There's a few syntax issues with the code in the question.
Are these functions... or methods in a class? I ask because the first params are 'self'. Leads me to believe these are methods in a class. In which case to call a method you'd do:
self.OnButtonClick2(3)
Upvotes: 0
Reputation: 23211
By your use of self
I assume you are inside a class. To call other class methods you want to use:
self.OnButtonClick2(3)
Upvotes: 0