簡體   English   中英

運行其他功能后如何在tkinter中覆蓋標簽

[英]How to overwrite label in tkinter, after running another functions

我第一次使用tkinter(python)制作GUI,我想在同一位置顯示結果,它們在開始的地方。

運行此代碼時,功能可以正常使用,但是無法在標簽中顯示結果。

該按鈕必須從Entries中獲取數據,並將其與下拉列表中的數據一起提供給函數。

結果應覆蓋列表as1,as2 = [0,0],然后在標簽result_1,result_2上顯示結果

我試圖將“ master”參數添加到函數中-onclick,但是隨后GUI運行時沒有單擊按鈕。

# coding=utf-8
from tkinter import *


def function_1(h, eta_bet):
    print(h, eta_bet)
    return h, eta_bet


def calculated_value_concrete(class_concrete):  # could be 20/25
    eta_bet = class_concrete
    return eta_bet


class Menu:
    def __init__(self, master):
        container = Label(master, bg="#003366")
        container.pack()

        menu_bg = Label(container, bg="#003366", fg="white", pady=15)
        countings_bg = Label(container)

        self.button1 = Button(menu_bg, text="Zbrojenie symetryczne", command=lambda: self.onclick(1, countings_bg),
                              width=20)

        menu_bg.pack(side=LEFT, fill=Y)

        self.button1.pack()

    def onclick(self, args, countings_bg):
        if args == 1:
            countings_bg.pack()
            ZbrojenieSymetryczne(countings_bg)


class ZbrojenieSymetryczne:
    def __init__(self, master):
        self.desc_1 = Label(master, text="Wysokość przekroju h [cm]")
        self.desc_7 = Label(master, text="Wybór betonu")
        self.data_1 = Entry(master, width=6)

        var = StringVar()
        var.set("Klasa")

        self.data_7 = OptionMenu(master, var, "1", "2", command=self.option_menu)
        self.data_7.config(width=10)

        self.desc_1.grid(row=1, sticky=E)
        self.desc_7.grid(row=7, sticky=E)

        self.data_1.grid(row=1, column=1)
        self.data_7.grid(row=7, column=1, stick="ew")

        self.button5 = Button(master, text="Count", command=self.onclick)
        self.button5.grid(row=9, columnspan=2, pady=10)

        as1, as2 = [0, 0]

        self.result_1 = Label(master, text=f"A_s1 = {as1} [cm^2]")
        self.result_1.grid(row=12, sticky=E)
        self.result_2 = Label(master, text=f"A_s2 = {as2} [cm^2]")
        self.result_2.grid(row=13, sticky=E)

    def option_menu(self, selection):
        self.eta_bet = calculated_value_concrete(selection)
        print(self.eta_bet)

    def onclick(self):
        h = float(self.data_1.get().replace(',', '.')) * 10 ** -2

        as1, as2 = function_1(h, self.eta_bet)

        self.result_1 = Label(master, text=f"A_s1 = {as1} [cm^2]")
        self.result_1.grid(row=12, sticky=E)
        self.result_2 = Label(master, text=f"A_s2 = {as2} [cm^2]")
        self.result_2.grid(row=13, sticky=E)


root = Tk()
root.title("Obliczanie zbrojenia")

Menu(root)
root.mainloop()

我希望結果與開始時的標簽相同(在按鈕下方)

如果要更新現有標簽的文本,有很多方法可以執行此操作,但也許可以考慮在onclick函數中執行此操作,而不是創建新按鈕。

def onclick(self):
    h = float(self.data_1.get().replace(',', '.')) * 10 ** -2

    as1, as2 = function_1(h, self.eta_bet)

    self.result_1['text'] = f"A_s1 = {as1} [cm^2]"
    self.result_2['text'] = f"A_s2 = {as2} [cm^2]"

這應該根據f字符串設置result_1和result_2的文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM