小编Mar*_*DLF的帖子

线程和tkinter

我听说Python中的线程不容易处理,而且它们与tkinter变得更加混乱.

我有以下问题.我有两个类,一个用于GUI,另一个用于无限过程.首先,我启动GUI类,然后是无限进程的类.我希望当你关闭GUI时,它也完成了无限的过程,程序结束了.

代码的简化版本如下:

import time, threading
from tkinter import *
from tkinter import messagebox

finish = False

class tkinterGUI(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):  
        global finish
        #Main Window
        self.mainWindow = Tk()
        self.mainWindow.geometry("200x200")
        self.mainWindow.title("My GUI Title")
        #Label
        lbCommand = Label(self.mainWindow, text="Hello world", font=("Courier New", 16)).place(x=20, y=20)
        #Start
        self.mainWindow.mainloop()
        #When the GUI is closed we set finish to "True"
        finish = True

class InfiniteProcess(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global finish
        while not finish:
            print("Infinite Loop")
            time.sleep(3)

GUI = tkinterGUI()
GUI.start()
Process …
Run Code Online (Sandbox Code Playgroud)

python multithreading tkinter tcl python-3.x

14
推荐指数
1
解决办法
8463
查看次数

Tcl_AsyncDelete错误多线程Python

我听说Python中的线程不容易处理,并且与tkinter更加纠结。

我有以下问题。我有两个类,一个用于GUI,另一个用于无限进程(我必须同时使用两个类)。首先,我启动GUI类,然后启动无限进程类。我希望当您关闭GUI时,它也完成无限过程,并且程序结束。

以下是该代码的简化版本:

import time, threading
from tkinter import *
from tkinter import messagebox

class Interface(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.attrib1 = "Attrib from Interface class"

    def run(self): 
        #Main Window
        self.mainWindow = Tk()
        self.mainWindow.geometry("200x200")
        self.mainWindow.title("My GUI Title")
        self.mainWindow.protocol("WM_DELETE_WINDOW", self.quit)
        #Label
        lbCommand = Label(self.mainWindow, text="Hello world", font=("Courier New", 16)).place(x=20, y=20)
        #Start
        self.mainWindow.mainloop()

    #The Interface class contains methods that use attributes from itself and attributes from Process class.
    def method1(self): 
        print(self.attrib1)
        print(SecondThread.attrib2)

    def quit(self):
        if messagebox.askyesno('App','Are you sure you want to quit?'):
            #In …
Run Code Online (Sandbox Code Playgroud)

python multithreading tkinter tcl

2
推荐指数
1
解决办法
7421
查看次数

标签 统计

multithreading ×2

python ×2

tcl ×2

tkinter ×2

python-3.x ×1