【问题标题】:How to display the value of function in tkinter?如何在 tkinter 中显示函数的值?
【发布时间】:2016-09-19 11:28:37
【问题描述】:

我是 python 和 tkinter 的新手,我决定做一个秒表。 我已经找到了很多有用的信息,但我仍然没有找到如何在 tkinter 中显示函数的值。这是我当前的代码:

import time
from tkinter import*
import os

root = Tk()

def clock(event):
    second = 0
    minute = 0
    hour = 0  
    while True:
        time.sleep(0.99)
        second +=1
        print(hour,":",minute,":",second)
    return

def stop(event):
    time.sleep(1500)

def clear(event):
    os.system('cls')


button1 = Button(root, text="Start")
button2 = Button(root, text="Stop")
button3 = Button(root, text="Clear")

button1.bind("<Button-1>", clock)
button2.bind("<Button-1>", stop)
button3.bind("<Button-1>", clear)

button1.grid(row=2, column=0, columnspan=2)
button2.grid(row=2, column=2, columnspan=2)
button3.grid(row=2, column=4, columnspan=2)


root.mainloop()

我知道代码还不完善(尤其是功能停止和清除)。

【问题讨论】:

    标签: python function python-3.x tkinter stopwatch


    【解决方案1】:

    从您的示例代码中不清楚您要显示哪个函数的值。

    无论如何,在tkinter 中完成类似操作的一个好方法是创建其StringVar Variable 类的实例,然后将它们指定为另一个小部件的textvariable 选项。完成此操作后,对 StringVar 实例值的任何更改都将自动更新关联小部件的文本。

    下面的代码说明了这一点:

    import os
    import time
    import tkinter as tk
    
    class TimerApp(tk.Frame):
        def __init__(self, master=None):
            tk.Frame.__init__(self, master=None)
            self.grid()
            self.create_widgets()
            self.elapsed = 0
            self.refresh_timer()
            self.after_id = None  # used to determine and control if timer is running
    
        def create_widgets(self):
            self.timer = tk.StringVar()
            self.timer.set('')
            self.timer_label = tk.Label(self, textvariable=self.timer)
            self.timer_label.grid(row=1, column=2)
    
            self.button1 = tk.Button(self, text="Start", command=self.start_clock)
            self.button1.grid(row=2, column=0, columnspan=2)
            self.button2 = tk.Button(self, text="Stop", command=self.stop_clock)
            self.button2.grid(row=2, column=2, columnspan=2)
            self.button3 = tk.Button(self, text="Clear", command=self.clear_clock)
            self.button3.grid(row=2, column=4, columnspan=2)
    
        def start_clock(self):
            self.start_time = time.time()
            self.after_id = self.after(1000, self.update_clock)
    
        def stop_clock(self):
            if self.after_id:
                self.after_cancel(self.after_id)
                self.after_id = None
    
        def clear_clock(self):
            was_running = True if self.after_id else False
            self.stop_clock()
            self.elapsed = 0
            self.refresh_timer()
            if was_running:
                self.start_clock()
    
        def update_clock(self):
            if self.after_id:
                now = time.time()
                delta_time = round(now - self.start_time)
                self.start_time = now
                self.elapsed += delta_time
                self.refresh_timer()
                self.after_id = self.after(1000, self.update_clock)  # keep updating
    
        def refresh_timer(self):
            hours, remainder = divmod(self.elapsed, 3600)
            minutes, seconds = divmod(remainder, 60)
            self.timer.set('{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds))
    
    app = TimerApp()
    app.master.title('Timer')
    app.mainloop()
    

    【讨论】:

      【解决方案2】:

      您可能会考虑使用回调函数(即在发生某些事情时调用您的函数——例如单击按钮时):

      引用Tkinter Callbacks的部分内容:

      在 Tkinter 中,回调是由 Tk 调用的 Python 代码,当 有事情发生。例如,Button 小部件提供 command 用户单击按钮时调用的回调。您还使用 带有事件绑定的回调。

      您可以使用任何可调用的 Python 对象作为 回调。这包括 普通函数、绑定方法、lambda 表达式可调用 对象。本文档简要讨论了这些备选方案中的每一个。
      ...

      要将函数对象用作回调,请将其直接传递给 Tkinter。

      from Tkinter import *
      
      def callback():
          print "clicked!"
      
      b = Button(text="click me", command=callback)
      b.pack()
      
      mainloop()
      

      【讨论】:

        猜你喜欢
        • 2016-04-04
        • 2019-10-27
        • 2017-12-01
        • 2021-10-02
        • 1970-01-01
        • 2014-12-02
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多