如何在Tkinter类上调用函数?(不在标签内)

时间:2014-01-21 12:02:39

标签: python 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]        
        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()

而且我的标签

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

哪个(我不知道为什么)不会出现在根甚至base = basemet上。求助(这是本周的一个项目)!

1 个答案:

答案 0 :(得分:2)

在Python中,函数不能修改调用者所感知的某些参数(整数,字符串),它们是不可变的。但是,像列表这样的对象是可变的。

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()