简体   繁体   English

再次按下按钮后,如何使 window 刷新?

[英]How do I make the window refresh after the button is pressed again?

I'm fairly new to python, and I wanted to make more advanced programs with a GUI.我是 python 的新手,我想用 GUI 制作更高级的程序。 This is some simple calculator that calculates profit on stocks at a certain percentage.这是一些简单的计算器,可以按一定百分比计算股票的利润。 It ask for the price and the amount of shares, then spits out the profit at a 1 - 15% increase.它询问价格和股份数量,然后以 1 - 15% 的增幅吐出利润。 But after you press enter again the label just prints over the previously printed labels.但是在您再次按下回车键后,label 只是打印在之前打印的标签上。 I tried to add a refresh button, but that didn't work either.我试图添加一个刷新按钮,但这也不起作用。

from tkinter import *

root = Tk()

root.geometry("1280x720")
root.title("Profit Calculator")

share_label = Label(root, text="Shares: ")
share_label.grid(row=1, column=0)

price_label = Label(root, text="Price: ")
price_label.grid(row=0, column=0)

shares = Entry(root,)
shares.grid(row=1, column=1)

price = Entry(root,)
price.grid(row=0, column=1)


def profit_calculator():
    cost = float(shares.get()) * float(price.get())
    overall_cost = Label(root, text=("Overall Cost: $" + "{:.2f}".format(cost)))
    overall_cost.grid(row=3, columnspan=2)
    counter_of_percent = 1

    for percent in range(1, 16):
        float(percent)
        percentage = percent / 100
        profit = cost * percentage

        sell_price = float(price.get()) * (percentage + 1)

        profit_at_percentage = Label(root, text="Sell Price at " + "{}".format(counter_of_percent) + "%: $" + "{:.2f}".format(sell_price) +
                                                "\nProfit at " + "{}".format(counter_of_percent) + "%: $" + "{:.2f}".format(profit))
        profit_at_percentage.grid(row=(4 + percent), columnspan=2)
        counter_of_percent += 1

    refresh = Button(root, text="Refresh", width=20, command=profit_at_percentage.destroy())
    refresh.grid(row=20, columnspan=2)


enter = Button(root, text="Enter", width=20, command=profit_calculator)
enter.grid(row=2, columnspan=2)

root.mainloop()

The key here is to create all the labels in advance, and then use the function to update the Label text instead of destroying previous Labels and recreating them.这里的关键是提前创建好所有的label,然后用function更新Label的文本,而不是破坏之前的Label重新创建。 For Labels like the header it is easy to just move it out of the function, but for labels that you create in the loop we need to add them all to a list to keep track of them.对于像 header 这样的标签,很容易将其移出 function,但对于您在循环中创建的标签,我们需要将它们全部添加到列表中以跟踪它们。

from tkinter import *

root = Tk()

root.geometry("1280x720")
root.title("Profit Calculator")

share_label = Label(root, text="Shares: ")
share_label.grid(row=1, column=0)

price_label = Label(root, text="Price: ")
price_label.grid(row=0, column=0)

shares = Entry(root,)
shares.grid(row=1, column=1)

price = Entry(root,)
price.grid(row=0, column=1)

overall_cost = Label(root)
overall_cost.grid(row=3, columnspan=2)

profit_labels = []
for percent in range(1, 16):
    profit_at_percentage = Label(root)
    profit_at_percentage.grid(row=(4 + percent), columnspan=2)
    profit_labels.append(profit_at_percentage)

def profit_calculator():
    cost = float(shares.get()) * float(price.get())
    overall_cost.config(text=("Overall Cost: $" + "{:.2f}".format(cost)))

    counter_of_percent = 1

    for lbl in profit_labels:
        float(percent)
        percentage = percent / 100
        profit = cost * percentage

        sell_price = float(price.get()) * (percentage + 1)

        lbl.config(text="Sell Price at " + "{}".format(counter_of_percent) + "%: $" + "{:.2f}".format(sell_price) +
                                                "\nProfit at " + "{}".format(counter_of_percent) + "%: $" + "{:.2f}".format(profit))
        counter_of_percent += 1

enter = Button(root, text="Enter", width=20, command=profit_calculator)
enter.grid(row=2, columnspan=2)

root.mainloop()

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

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