user3216984
user3216984

Reputation: 27

How to call a function on Tkinter class?(not inside label)

I'm trying to use a function which a placed inside the class of Tkinder but the function(aktodec) can't be found and a get an error. I don't wanna call the def as a command of a button but as a function that will give value to one of my variables

from Tkinter import *
class ADialog:
    def __init__(self, parent):
        top = self.top = Toplevel(parent)
        Label(top, text="Number to convert").pack()
        self.numb = Entry(top)
        self.numb.pack(padx=15)
        Label(top, text="Base of incered number").pack()
        self.base = Entry(top) 
        self.base.pack(padx=15)
        Label(top, text="Base you want to be converted").pack()
        self.basemet=Entry(top)
        self.basemet.pack(padx=15)
        b = Button(top, text="OK", command=self.met)
        b.pack(pady=5)
    def aktodec(self,num,base): #####commands
        dec=0
        num=num[::-1]        
        for i in range(len(num)):
            s=int(num1[i])*(int(base)**i)
            dec=dec+s        
    def met(self):
        num=self.numb=str(self.numb.get())
        base=self.base =int(self.base.get())
        basemet=self.basemet=int(self.basemet.get())
        if base==basemet:
            Label(root,text="The number "+self.numb+"is converted to"+self.numb) ##why can't be print??
        if base==10:
            new=num    
        else:
            new1=self.aktodec(num,base)           ####why aktodec doesn't give value to "new"?? 
        Label(root,text="Number is"+str(new))       
        self.top.destroy()

root = Tk()
def open_dialog():
    dial = ADialog(root)
    root.wait_window(dial.top)

root.wm_geometry("400x300+20+40")
message=StringVar()
message.set("Complete the form")
Label(root, textvariable=message).pack(padx=30)
root.update()
message.set("Form completed")
Button(root, text="Done", command=root.destroy).pack()
Button(root, text="new", command=open_dialog).pack()
root.update()
root.mainloop()

And also I have a problem whith the label

Label(root,text="The number "+self.numb+"is converted to"+self.numb

which (i don't know why) won't appear to the root even the base=basemet. Help please(it's for a project in this week)!

Upvotes: 0

Views: 1435

Answers (1)

afkfurion
afkfurion

Reputation: 2865

In Python, a function can't modify some arguments(integers, strings) as perceived by the caller, they are immutable. However, some objects like lists are mutable.

def aktodec(self,num,base): 
    dec=0
    num=num[::-1]        
    for i in range(len(num)):
        s=int(num1[i])*(int(base)**i)
        dec=dec+s      
    return dec 
def met(self):
    num=self.numb=str(self.numb.get())
    base=self.base =int(self.base.get())
    basemet=self.basemet=int(self.basemet.get())
    new = num
    if base==basemet:
        Label(root,text="The number "+self.numb+"is converted to"+self.numb).pack()
    if base==10:
        new=num    
    else:
        new=self.aktodec(num,base)          
    Label(root,text="Number is"+str(new)).pack()      
    self.top.destroy()

Upvotes: 3

Related Questions