如何为Python / TKinter中的按钮分配函数?

时间:2016-02-14 21:01:43

标签: python user-interface python-3.x tkinter

我对TKinter很新,并且一直在尝试将我的普通Python代码转换为GUI(TKinter代码)!我一直在研究这个代码,到目前为止,我已经完成了基本布局,但是我在编码按钮和使用条目方面遇到了问题。您很可能会在我的代码中发现很多错误,因此请注意! :d

我在窗口顶部有一个条目,我希望用户在条目中输入一个数字,然后我想在某些代码(btn1())中使用条目中输入的文本。我还希望用户按下按钮然后按钮运行一些带有标签的代码,按钮显示代码的结果(btn1()功能中的标签)。

首先,我希望用户在条目中输入一个数字。然后,我希望用户单击条目下方的按钮。最后,我希望按钮后面的代码结果显示在按钮下方(标签中!)。

这是我的代码:

from tkinter import *

class window_design:

def __init__(self):
    root=Tk()
    root.title("Bag Weight")
    root.geometry("500x700")
    root.wm_iconbitmap('favicon.ico')


    image=PhotoImage(file="Weight Program.png")
    imagelabel=Label(root,image=image)
    imagelabel.pack()


    weightentrylabel=Label(root,text="Enter Weight!")
    weightentrylabel.pack()

    self.string=StringVar()
    weightentry=Entry(root,textvariable=self.string)
    weightentry.pack()

    menutext=Label(root,text="What coin are you using?")
    menutext.pack(side=LEFT)

    values=['1p','2p','5p','10p','20p','50p','£1','£2','Exit']

    def btn1(self,btn1code):
        p1=3.56
        p1should=356
        if (self.string.get()) > p1should:
            weightdif=(self.string.get())-p1should
            coins=weightdif/p1
            labeldif=Label(text=weightdif)
            labelcoins=Label(text=coins)
        elif (self.string.get()) < p1should:
            weightdif=p1should-(self.string.get())
            coins=weightdif/p1
            labeldif=Label(text=weightdif)
            labelcoins=Label(text=coins)

    button1=Button(root,text="1p",command=btn1)
    button1.pack(side=LEFT)



    root.mainloop()



window_design()

我目前收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\cjay2\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
TypeError: btn1() missing 2 required positional arguments: 'self' and 'btn1code'

3 个答案:

答案 0 :(得分:2)

您应该在self.btn1中使用btn1button1=Button(root,text="1p",command=btn1)是一种类方法)。

使用一个参数调用

btn1(),它需要两个参数,将默认值设置为btn1code或removeit(如果不使用它)。

当您在get()上调用StringVar()方法时,它会返回一个字符串,因此您需要在与整数进行比较之前进行转换。

要在label中显示结果,请使用self.result = StringVar(),然后致电self.result.set(a_string)。 请检查以下代码:

from tkinter import *

class window_design:

    def __init__(self):
        root=Tk()
        root.title("Bag Weight")
        #root.geometry("500x700")
        root.wm_iconbitmap('favicon.ico')


        image=PhotoImage(file="Weight Program.png")
        imagelabel=Label(root,image=image)
        imagelabel.pack()


        weightentrylabel=Label(root,text="Enter Weight!")
        weightentrylabel.pack()

        self.string=StringVar()
        weightentry=Entry(root,textvariable=self.string)
        weightentry.pack()

        menutext=Label(root,text="What coin are you using?")
        #menutext.pack(side=LEFT)
        menutext.pack()

        values=['1p','2p','5p','10p','20p','50p','£1','£2','Exit']

        button1=Button(root,text="1p",command=self.btn1)
        #button1.pack(side=LEFT)
        button1.pack()

        #--------------------------------------------------
        self.result=StringVar()
        resultlabel=Label(root, textvariable = self.result)
        resultlabel.pack()
        #--------------------------------------------------

        root.mainloop()
    #-------------------------------------  
    def btn1(self):
        p1=3.56
        p1should=356
        if not self.string.get(): return

        value = int(self.string.get())
        if value > p1should:
            weightdif = value - p1should
            coins=weightdif/p1

        elif value < p1should:
            weightdif=p1should - value
            coins=weightdif/p1

        self.result.set(coins)
    #-----------------------------------



window_design()

答案 1 :(得分:1)

您将btn1()定义为除了self之外还需要参数,但是Tkinter在没有参数的情况下调用它。看起来您甚至没有使用btn1code,因此您可以将功能定义更改为def btn1(self):

答案 2 :(得分:0)

你可以将btn1code声明为成员变量,并将其从函数定义中删除,或者如果你想使用带有Button参数的调用函数;像这样使用lambda函数:

button1=Button(root,text="1p",command=lambda: btn1(btn1code))