如何在 Python/TKinter 中为按钮分配功能?

问题描述 投票:0回答:4

我对 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'
python user-interface python-3.x tkinter
4个回答
2
投票

您应该在

self.btn1
中使用
btn1
button1=Button(root,text="1p",command=btn1)
是一个类方法)。

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

当您在

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
投票

您将

btn1()
定义为除了
self
之外还需要一个参数,但 Tkinter 调用它时没有参数。看来您甚至没有使用
btn1code
,因此您可以将函数定义更改为
def btn1(self):


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.