如何将标签小部件放在一起?

时间:2017-04-05 10:52:10

标签: python tkinter

我正在创建一个简单的GUI程序来测量某个事件的时间。一切都很好,但有一件事让我感到困惑 - 所有标签小部件都是"闪烁" (由于被创建),所以我想重构我的代码,以便我有两个标签组 - 其中一个将不断显示,另一个(实际测量时间)将闪烁。问题是,当我尝试将标签分成两个较小的标签时,我无法将其标记在另一个标签旁边,所以看起来像这样:

enter image description here

这是我原来的工作代码:

# str8.py
#   Program to count time from a certain event

from tkinter import *
from datetime import *
from threading import *

def display():

    event, today, str8, seconds, minutes, hours, days, weeks, years = calc()

    Label(root,
          text = "You have been STR8 for:\n",
          font = "Verdana 8 bold").grid(row = 0, sticky = W)

    Label(root,
          text = "Years: "
               + str(round(years, 2)),
          font = "Verdana 8").grid(row = 1, sticky = W)

    Label(root,
          text = "Weeks: "
               + str(round(weeks, 2)),
          font = "Verdana 8").grid(row = 2, sticky = W)

    Label(root,
          text = "Days: "
               + str(round(days, 2)),
          font = "Verdana 8").grid(row = 3, sticky = W)

    Label(root,
          text = "Hours: "
               + str(round(hours, 2)),
          font = "Verdana 8").grid(row = 4, sticky = W)

    Label(root,
          text = "Minutes: "
               + str(round(minutes, 2)),
          font = "Verdana 8").grid(row = 5, sticky = W)

    Label(root,
          text = "Seconds: "
               + str(round(str8.total_seconds())),
          font = "Verdana 8").grid(row = 6, sticky = W)

    Button(root,
           text = "EXIT",
           font = "Verdana 8",
           height = 1,
           width = 19,
           command = quit).grid(row = 7)


def calc():

    event = datetime(2017, 4, 4, 0, 0, 0)
    today = datetime.now()

    str8 = today - event

    seconds = str8.total_seconds()
    minutes = str8.total_seconds() / 60
    hours = minutes / 60
    days = hours / 24
    weeks = days / 7
    years = weeks / 52

    return event, today, str8, seconds, minutes, hours, days, weeks, years


def print_it():
    t = Timer(1.0, print_it)
    calc()
    try:
        display()
    except RuntimeError:
        pass
    else:
        t.start()

def quit():
    root.destroy()

if __name__ == '__main__':
    root = Tk()
    root.title("STR8")
    root.resizable(width = False, height = False)
    print_it()
    root.mainloop()

...之前我尝试将其中一个分开试试:

Label(root,
      text = "Years: ",
      font = "Verdana 8").grid(row = 1, sticky = W)

Label(root,
      text = str(round(years, 2)),
      font = "Verdana 8").grid(row = 1, column = 1, sticky = W)

然后我会将所有正在显示的标签不断放入create_widgets()函数中,并将其他标签保留在display()函数中。

我使用的是Python 3.5。

1 个答案:

答案 0 :(得分:1)

TKinter有一个名为DoubleVar的{​​{3}},可让您创建一个可用于更新Label小部件的变量。使用此方法,而不是使用text=作为标签,您可以使用textvariable=来引用您创建的变量,并且Tk知道在变量更改值时更新标签(尽管应该注意还有其他方法可以实现更新标签,我在这里不详细说明。

在下面的代码中,我们为每个时间单位创建两个文本标签 - 一个用于告诉用户该值与哪个值相关,另一个用于实际显示该值。为简单起见,我通过字典完成了这项工作。

然后我们第一次调用increment,它会设置所有相关值。执行此操作后,我们使用self.after(1000, self.increment)在1000毫秒= 1秒后运行增量过程。

# str8.py
#   Program to count time from a certain event

from tkinter import *
from datetime import *


class App(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.grid(sticky=N + W + E + S)

        Label(self, text='You have been STR8 for:', font="Verdana 8 bold").grid(row=0, sticky=W)

        self.counters = dict()
        measurements = ['Seconds', 'Minutes', 'Hours', 'Days', 'Weeks', 'Years']
        for i, measurement in enumerate(measurements):
            self.counters[measurement] = DoubleVar()
            Label(self, text=measurement, font='Verdana 8').grid(row=i+1, column=0, sticky=W)
            Label(self, textvariable=self.counters[measurement], font='Verdana 8').grid(row=i + 1, column=1, sticky=E)
            self.counters[measurement].set(0)

        Button(self,
               text="EXIT",
               font="Verdana 8",
               height=1,
               width=19,
               command=quit).grid(row=7, column=0)

        self.increment()

    def increment(self):
        event = datetime(2017, 4, 4, 0, 0, 0)
        today = datetime.now()

        str8 = today - event
        self.counters['Seconds'].set(round(str8.total_seconds(), 2))
        self.counters['Minutes'].set(round(str8.total_seconds()/60, 2))
        self.counters['Hours'].set(round(str8.total_seconds() / 3600, 2))
        self.counters['Days'].set(round(str8.total_seconds() / (3600 * 24), 2))
        self.counters['Weeks'].set(round(str8.total_seconds() / (3600 * 24 * 7), 2))
        self.counters['Years'].set(round(str8.total_seconds() / (3600 * 24 * 7 * 52), 2))

        self.after(1000, self.increment)


if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.title("STR8")
    root.resizable(width=False, height=False)
    app.mainloop()

这会产生如下所示的窗口:

variable class

并且应该每秒更新而不闪烁。