简体   繁体   English

如何在 tkinter 中将 label 文本与 label 值并排保存

[英]How do I keep label text side by side with label value in tkinter

I am trying to keep the label text value: "This is the subtotal" next to subtotal value.我试图保留 label 文本值:“这是小计”旁边的小计值。 Meaning:意义:

在此处输入图像描述

If I were to click on the "Calulate Subtotal" Button the text "This is the subtotal" should be to the right and the actual subtotal should be to the left.如果我点击“计算小计”按钮,文本“这是小计”应该在右边,实际小计应该在左边。 Currently, If I were to click on the "Calulate Subtotal" Button the text "this is the subtotal" disappears.目前,如果我点击“计算小计”按钮,“这是小计”的文本就会消失。 Can someone steer me in the right direction?有人可以引导我朝着正确的方向前进吗?

try:
import Tkinter as tk
except:
  import tkinter as tk

class GetInterfaceValues():
    def __init__(self):
    self.root = tk.Tk()
    self.root.geometry('500x200')
    self.button = tk.Button(self.root,
                            text='Calculate Subtotal',
                            command=self.getSubtotals)
    self.button.pack()

    self.firstLabel = tk.Label(self.root, text="This is the subtotal:")

    self.firstLabel.pack()

    self.root.mainloop()

def getSubtotals(self):
    self.firstLabel["text"] = 55*10



 app = GetInterfaceValues()

You can simply change your getSubtotals method to retain the current text of firstLabel as the following:您可以简单地更改您的getSubtotals方法以保留firstLabel的当前文本,如下所示:

def getSubtotals(self):
    self.firstLabel["text"] = self.firstLabel["text"] + str(55 * 10)

Couple of suggestions:几个建议:

  • You might want to create another widget to show subtotal value other than firstLabel .您可能想要创建另一个小部件来显示除firstLabel之外的小计值。
  • You might want to restructure your class so that you only initialize the attributes in the init method您可能需要重组 class 以便仅在 init 方法中初始化属性
  • Please check the indentations and code formatting when asking questions to make it easier for others to inspect your code请在提问时检查缩进和代码格式,以便其他人更容易检查您的代码

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

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