繁体   English   中英

Tkinter 程序运行 function 但 window 结束后弹出

[英]Tkinter program runs function but the window pops up after it ends

好的,所以我对 tkinter 很陌生,我无法解决我遇到的这个问题。

当我运行程序时,它运行 function 并在结束 window 并弹出照片后,但循环不会再次启动程序。

import tkinter as tk
from tkinter import*
from PIL import ImageTk,Image 
from tkinter import messagebox
import YuaChanMainFunc
import time

def on_click(event=None):
    # `command=` calls function without argument
    # `bind` calls function with one argument
    print("Hey Yua!")
    YuaChanMainFunc.statement="hey yua"

class Window(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        master.title("Yua-chan AI")
        self.img = ImageTk.PhotoImage(Image.open("YuaChanAI/Yua Chan Artwork/YuaChan2.png"))
        MainLB = tk.Label(master, image=self.img)
        MainLB.bind('<Button-1>', on_click)
        MainLB.pack()
        b = tk.Button(root, text="Close", command=root.destroy)
        b.pack()



#YuaChanMainFunc.YuaChanAIMainFunc()
root = tk.Tk()
#instance of the class
app = Window(root)
root.resizable(0,0)
root.geometry("310x500+1600+510")
YuaChanMainFunc.YuaChanAIMainFunc()
#Runs the application until we close
root.mainloop()

据我了解,您希望“YuaChanMainFunc.YuaChanAIMainFunc()”在后台运行,而 UI 运行在前台。 为此,您可以在不同的线程中启动“YuaChanMainFunc.YuaChanAIMainFunc()”并在主线程本身中运行 UI。 现在你可以让“YuaChanMainFunc.YuaChanAIMainFunc()”成为一个无限循环,它不会阻塞 root.mainloop()。 还要记住 root.mainloop() 也是一个无限循环。 因此,在您关闭程序之前,您之后编写的任何内容都不会执行。

import threading
backend_thread = threading.Thread(target=YuaChanMainFunc.YuaChanAIMainFunc, args=())
backend_thread.daemon = True  #This will make sure backend thread closes when you close ui
backend_thread.start()
root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM