【问题标题】:Python - tkinter button freezePython - tkinter 按钮冻结
【发布时间】:2021-07-04 05:58:36
【问题描述】:

我在 Python 中冻结应用程序时遇到问题。我使用 tkinter 库制作了一些应用程序。当我使用发送按钮时,它会调用持续 3 分钟的功能并在这 3 分钟内冻结应用程序......我想查看来自该功能的所有日志“实时”。我不能等待 3 分钟,然后获取所有日志。

以下是我的代码示例:

import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
import time


class Frames:

    def main_frame(self, win):

        # Main Frame
        main = ttk.LabelFrame(win, text="")
        main.grid(column=0, row=0, sticky="WENS", padx=10, pady=10)
        return main

    def button_frame(self, win):

        # Button Frame
        butt_frame = ttk.LabelFrame(win, text="Button")
        butt_frame.grid(column=0, row=0, sticky='NWS')

        def _send_button():
            for i in range(10):
                self.write_text_console("{0}\n".format(i))
                time.sleep(0.5)

        # Send Button
        ttk.Label(butt_frame, text="                                         ").grid(column=0, row=8)
        button = tk.Button(butt_frame, text="Send", command=_send_button, foreground='black')
        button.grid(column=0, row=13, sticky=tk.EW)

    def scrolled_text_widget(self, win):
        ttk.Label(win, text="                                         ").grid(column=0, row=1)
        ttk.Label(win, text="Console Output:").grid(column=0, row=2, sticky=tk.W)
        self.scr = scrolledtext.ScrolledText(win, width=100, height=10, wrap=tk.WORD, state="disabled")
        self.scr.grid(column=0, row=3, columnspan=5)

    def write_text_console(self, string, color="black", tag="console"):
        self.scr.configure(state='normal')
        self.scr.insert('end', string, tag)
        self.scr.tag_config(tag, foreground=color)
        self.scr.configure(state='disabled')
        self.scr.see("end")


win = tk.Tk()
win.geometry("845x300")
win.resizable(0, 0)
frames = Frames()
main = frames.main_frame(win)
frames.button_frame(main)
frames.scrolled_text_widget(main)
win.mainloop()

这个例子显示了我的问题。当您单击发送按钮时,它会冻结应用程序 5 秒。但我需要在循环期间查看日志。

我该如何解决?

【问题讨论】:

  • 重新编写您的代码,使其适用于win.after()。为什么不使用__init__ 并在其中定义win 之类的属性,这样它就可以在任何地方使用,而不是在每个方法上传递它。

标签: python tkinter


【解决方案1】:

Tkinter 在你的主线程中运行一个循环,这就是为什么当你点击一个按钮时你的应用程序冻结的原因。解决办法是新建一个线程。

1- 你必须导入线程

import threading

2- 在 _send_button() 函数中启动一个新线程。应该是这样的。

 def _send_button():

        def click_button():
            for i in range(10):
                self.write_text_console("{0}\n".format(i))
                time.sleep(0.5)

        threading.Thread(target=click_button).start()

Learn More About Threading In Python

【讨论】:

  • 虽然这适用于小代码,但tkinter 是单线程的,它不能在两个线程之间进行通信。
  • 我在考虑线程,但我以前没用过。感谢您的回答和链接,我可以在其中了解更多信息!
【解决方案2】:

要冻结按钮,请在特定按钮小部件中添加state= DISABLED

from tkinter import *

#Create an instance of tkiner frame
root= Tk()

#Define the geometry of the function
root.geometry("400x250")

Button(root, text="OK", state= DISABLED).pack(pady=20)

root.mainloop()

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多