在Tkinter上单击按钮后如何更改标签的值 [英] how to change value of label after button is clicked on Tkinter

查看:697
本文介绍了在Tkinter上单击按钮后如何更改标签的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的银行帐户GUI,当我单击菜单兴趣"时,变量从1更改为2,这应将当前余额的值更改10%,但是该值保持不变,请输入洞察力.

I am creating a simple bank account GUI , when i click the menu " interest" the variable changed from 1 to 2 , which should change the value of the current balance by 10 percent, however the value stays the same, give your insight.

from tkinter import *
from random import randint


class BankAccount(object):
    def __init__(self, initial_balance=0):
        self.balance = initial_balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount       
    def get_balance(self, initial_balance, rate):
        return self.get_balance() * self._rate

class BankAccountWithInterest(BankAccount):
    def __init__(self, initial_balance=0, rate=0.1):
        BankAccount.__init__(self, initial_balance)
        self._rate = rate           
    def interest(self):
        return self.balance * self._rate

balance = (randint(100, 500))
my_account = BankAccount(balance)
my_interest = BankAccountWithInterest(balance)
interest = my_interest.balance + my_interest.interest()

typeOfAccount = "1"
class GUI:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()


        #Toolbar#

        toolbar = Frame(root)
        toolbar.pack(side=TOP, fill=X)

        #Button#

        button1 = Button(toolbar, text="Deposit", width = 13,    command=self.depositBalance)
        button2 = Button(toolbar, text="Withdraw",width = 13, command=self.depositWithdraw)
        button1.pack(side=LEFT)
        button2.pack(side=RIGHT)

        #Menu#

        subMenu = Menu(menu)
        menu.add_cascade(label="Type of Account", menu=subMenu)
        subMenu.add_command(label="Standard", command= self.standard)
        subMenu.add_command(label="Interest", command= self.interest)

        #Textbox#

        self.text = Entry(root)
        self.text.pack()

        #Labels#

        w = Label(root, text="Current Balance:")
        w.pack()
        w1 = tkinter.StringVar()
        if typeOfAccount == "1":
            w1 = Label(root, text=my_account.balance)
            w1.pack()
        elif typeOfAccount == "2":
            w1.set(text=interest)
            w1.pack()


    def depositBalance(self):
            a = int(self.text.get())
            my_account.balance = a + my_account.balance
            print(my_account.balance)

    def depositWithdraw(self):
            a = int(self.text.get())
            my_account.balance = my_account.balance - a
            print(my_account.balance)         

    def standard(self):
        typeOfAccount = "1"

    def interest(self):
        typeOfAccount = "2"


root = Tk()
menu = Menu(root)
root.config(menu=menu)
root.title("Bank Account")
root.minsize(width=250, height=100)
root.maxsize(width=300, height=150)
GUI(root)

root.mainloop()

推荐答案

您的代码有几个问题.多个类文件似乎没有按照您认为的那样工作,并且有些错误.我已经将您的程序重新设计为我认为您正在尝试的程序,并且我将所有内容都移入了一个大类中,因为在这种情况下它更有意义.通过在类之外创建变量并创建多个类,您使事情变得比原本要复杂的多.使用多个类并没有什么真正的错,但是您要使用的方法使事情变得比原来需要的要困难得多.

There are several problems with your code. The multiple class files do not appear to work as you think they should and some things are just wrong. I have reworked your program into what I think it is you are trying to do and I have moved everything into 1 big class as it made more sense in this situation. You were making things more complicated than it needed to be by creating variables outside of the classes and by making several classes. There is nothing really wrong with using several classes but the way you were going about it was making things way more difficult than it needed to be.

我要做的第一件事是创建我们将要使用的所有类变量/属性.

First thing I did was to create all the class variables/attributes that we are going to use.

为了使事情变得更容易,我通过将self.作为所有变量/小部件名称的前缀,使每个小部件和变量都成为类属性.这将使我们能够与任何类方法进行交互并更改每个属性而不会出现问题,并且如果您日后添加更多选项,则这些属性将已经定义并可以更改.

To make things easier on us going forward I made every widget and variable a class attribute by placing self. as a prefix to all the variable/widget names. This will allow us to interact with and change each attribute in any of the class methods without an issue and if you add more options down the road the attributes will already be defined and ready to change.

接下来,我将所有单独的类方法移至主类中,以简化操作.

Next I moved all your separate class methods into the main class to make things easier to work with.

我将有关帐户类型的if/else语句替换为一种方法.每当帐户类型更改或从余额中添加或删除金额时,这将使我们能够更新标签以显示余额或利息.

I replaced your if/else statement on the account types into a methods. This will allow us to update label to show the balance or the interest any time the account type changes or an amount is added or removed from the balance.

我对depositBalancedepositWithdraw方法进行了修改,以处理一些错误,因为您的输入字段不仅限于数字,而且如果用户放入其他内容或将其留空,则可能导致错误.

I modified the depositBalance and depositWithdraw methodsto have a little error handling because your entry field is not restricted to only numbers and can cause errors if the user puts anything else in or leaves it blank.

我更改standardinterest方法来更新typeOfAccount属性并通过调用type_account方法来更新标签.

I change the standard and interest methods to update the typeOfAccount attribute and to update the label by calling on the type_account method.

最后但并非最不重要的一点是常规清理,因此代码没有毫无意义的间距,并确保我们遵循DRY(不要重复自己)标准.

Last but not least some general clean up so the code did not have pointless spacing and to make sure we follow a DRY (don't repeat yourself) standard.

下面是我上面提到的所有更改的重新编写的代码.让我知道这是否有帮助,以及您是否对任何事情感到困惑.

Below is the reworked code with all the changes I mentioned above. Let me know if this helps and if you are confused on anything.

from tkinter import *
from random import randint

class GUI:
    def __init__(self, master):

        self.master = master
        self.typeOfAccount = "1"
        self.balance = (randint(100, 500))
        self.rate = 0.1

        self.frame = Frame(master)
        self.frame.pack()

        self.toolbar = Frame(root)
        self.toolbar.pack(side=TOP, fill=X)

        self.button1 = Button(self.toolbar, text="Deposit", width = 13, command=self.depositBalance)
        self.button2 = Button(self.toolbar, text="Withdraw",width = 13, command=self.depositWithdraw)
        self.button1.pack(side=LEFT)
        self.button2.pack(side=RIGHT)

        self.menu = Menu(self.master)
        self.master.config(menu = self.menu)
        self.subMenu = Menu(self.menu)
        self.menu.add_cascade(label="Type of Account", menu=self.subMenu)
        self.subMenu.add_command(label="Standard", command=self.standard)
        self.subMenu.add_command(label="Interest", command=self.interest)

        self.text = Entry(self.master)
        self.text.pack()

        self.w = Label(root, text="Current Balance: {}".format(self.balance))
        self.w.pack()
        #removed "tkinter." not needed because of the type of import you used
        self.w1 = StringVar()

    def type_account(self):
        if self.typeOfAccount == "1":
            self.w.config(text="Current Balance: {}".format(self.balance))

        elif self.typeOfAccount == "2":
            interest = self.balance * self.rate
            self.w.config(text="Current Balance: {}".format(interest))

    def depositBalance(self):
        try:
            if int(self.text.get()) > 0:
                a = int(self.text.get())
                self.balance = a + self.balance
                self.type_account()
        except:
            print("Blank or non numbers in entry field")

    def depositWithdraw(self):
        try:
            if int(self.text.get()) > 0:
                a = int(self.text.get())
                self.balance = self.balance - a
                self.type_account()
        except:
            print("Blank or non numbers in entry field")

    def standard(self):
        self.typeOfAccount = "1"
        self.type_account()

    def interest(self):
        self.typeOfAccount = "2"
        self.type_account()

if __name__ == "__main__":

    root = Tk()
    root.title("Bank Account")
    root.minsize(width=250, height=100)
    root.maxsize(width=300, height=150)

    app = GUI(root)

    root.mainloop()

这篇关于在Tkinter上单击按钮后如何更改标签的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆