我试图切片,现在我的标签不会显示..
首先我得到一个具有默认浮点值的DoubleVar。然后我尝试将更新后的值转换为字符串,然后对其进行切片并将值设置为标签,其结果来自条目和OptionsMenu。
我试图实现的是始终在resultlabel1中获得一个带有3位小数的值,当条目和OptionsMenu更改时会更新。
我正在谈论的代码:
self.resultlabel1 = DoubleVar()
self.resultlabel1.set(float(0.000))
self.label1 = Label(root, textvariable=str(self.resultlabel1)[:5])
self.label1.grid(row=2, column=3, columnspan=1, sticky='WE')
我的总代码在这里:
from tkinter import *
from decimal import *
class App(Frame):
def __init__(self, root=None):
Frame.__init__(self, root)
self.materialPrice = {'Brick': 70, 'Rockwool': 50, 'Concrete': 20}
materialvariable1 = StringVar(self, root)
materialvariable1.set("Choose material")
materialvariable2 = StringVar(self, root)
materialvariable2.set("Choose materiale")
self.w1 = OptionMenu(root, materialvariable1, *self.materialPrice, command=self.displayPrice)
self.w1.grid(row=2, column=0, columnspan=1, sticky='WE')
self.w2 = OptionMenu(root, materialvariable2, *self.materialPrice, command=self.displayPrice2)
self.w2.grid(row=3, column=0, columnspan=1, sticky='WE')
# --------------------------------------------------------------------------------------------------------------------
self.var = DoubleVar()
self.var.set(Decimal("0.100"))
self.var2 = DoubleVar()
self.var2.set(Decimal("0.200"))
self.entry1 = Entry(root, textvariable=self.var)
self.entry1.grid(row=2, column=1)
self.entry2 = Entry(root, textvariable=self.var2)
self.entry2.grid(row=3, column=1)
# --------------------------------------------------------------------------------------------------------------------
self.priceVarLabel1 = DoubleVar()
self.priceVarLabel1.set(float(0.00))
self.priceVarLabel2 = DoubleVar()
self.priceVarLabel2.set(float(0.00))
self.priceVarValue1 = Label(root, textvariable=self.priceVarLabel1, relief='sunken')
self.priceVarValue1.grid(row=2, column=2, columnspan=1, sticky='WE')
self.priceVarValue2 = Label(root, textvariable=self.priceVarLabel2, relief='sunken')
self.priceVarValue2.grid(row=3, column=2, columnspan=1, sticky='WE')
self.resultlabel1 = DoubleVar()
self.resultlabel1.set(float(0.000))
self.resultlabel2 = DoubleVar()
self.resultlabel2.set(float(0.00))
# This is what I got so far!
self.label1 = Label(root, textvariable=str(self.resultlabel1)[:5])
self.label1.grid(row=2, column=3, columnspan=1, sticky='WE')
self.label2 = Label(root, textvariable=self.resultlabel2)
self.label2.grid(row=3, column=3, columnspan=1, sticky='WE')
# --------------------------------------------------------------------------------------------------------------------
self.resultSumNew = DoubleVar()
#self.resultSumNew.set(float(0.00))
self.resultSumNew.set(Decimal("0.000"))
self.labelSum = Label(root, textvariable=self.resultSumNew)
self.labelSum.grid(row=4, column=1, columnspan=1, sticky='WE')
def set_label(name, index, mode):
self.resultSumNew.set(self.var.get() + self.var2.get())
self.var.trace('w', set_label)
self.var2.trace('w', set_label)
# --------------------------------------------------------------------------------------------------------------------
def displayPrice(self, value):
self.resultlabel1.set(self.var.get() / self.materialPrice[value])
self.priceVarLabel1.set(self.materialPrice[value])
def displayPrice2(self, value):
self.resultlabel2.set(self.var2.get() / self.materialPrice[value])
self.priceVarLabel2.set(self.materialPrice[value])
root = Tk()
app = App(root)
root.title("help")
root.mainloop()
答案 0 :(得分:1)
我能够这样做。将StringVar
用于标签文本,然后将其切片
self.resultlabel1 = DoubleVar()
self.resultlabel1.set(float(0.000))
self.resultlabel2 = DoubleVar()
self.resultlabel2.set(float(0.00))
self.resultlabel1_str = StringVar()
self.resultlabel1_str.set(str(self.resultlabel1.get()))
self.label1 = Label(root, textvariable=self.resultlabel1_str)
self.label1.grid(row=2, column=3, columnspan=1, sticky='WE')
self.label2 = Label(root, textvariable=self.resultlabel2)
self.label2.grid(row=3, column=3, columnspan=1, sticky='WE')
在你的职能中,
def displayPrice(self, value):
self.resultlabel1.set(self.var.get() / self.materialPrice[value])
self.priceVarLabel1.set(self.materialPrice[value])
self.resultlabel1_str.set(str(self.resultlabel1.get())[:5])
def displayPrice2(self, value):
self.resultlabel2.set(self.var2.get() / self.materialPrice[value])
self.priceVarLabel2.set(self.materialPrice[value])
希望这有帮助。