如何在Tkinter类上调用函数?

时间:2014-01-20 22:44:30

标签: class python-2.7 tkinter function

我正在尝试使用放置在Tkinder类中的函数但是找不到函数(aktodec)并且得到错误。我不想把def称为按钮的命令,而是作为一个能给我的一个变量赋值的函数

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]
        num1=[]
        for i in range(len(num)):
            num1.append(num[i])        
        if base>9:
            for i in range(len(num1)):   
                if str(num1[i])=='A':
                    num1.remove(num1[i])
                    num1.insert(i,'10')
                if str(num1[i])=='B':
                    num1.remove(num1[i])
                    num1.insert(i,'11')
                if str(num1[i])=='C':
                    num1.remove(num1[i])
                    num1.insert(i,'12')
                if str(num1[i])=='D':
                    num1.remove(num1[i])
                    num1.insert(i,'13')
                if str(num1[i])=='E':
                    num1.remove(num1[i])
                    num1.insert(i,'14')
                if str(num1[i])=='F':
                    num1.remove(num1[i])
                    num1.insert(i,'15')
            for i in range(len(num)):
                s=int(num1[i])*(int(base)**i)
                dec=dec+s
        else:
            for i in range(len(num1)):
                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())
        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()

而且我的标签

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

哪个(我不知道为什么)不会出现在根甚至base = basemet上。 求救!

0 个答案:

没有答案