简体   繁体   English

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

[英]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. 我正在尝试使用放在Tkinder类中的函数但是找不到函数(aktodec)并且得到错误。 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 我不想把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()

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. 哪个(我不知道为什么)不会出现在根甚至base = basemet上。 Help please(it's for a project in this week)! 求助(这是本周的项目)!

In Python, a function can't modify some arguments(integers, strings) as perceived by the caller, they are immutable. 在Python中,函数不能修改调用者所感知的某些参数(整数,字符串),它们是不可变的。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM