Python Tkinter程序运行函数,但窗口在结束后弹出
好吧,我对tkinter还很陌生,我不能解决我现在遇到的这个问题 当我运行程序时,它运行函数,在结束后,弹出带有照片的窗口,但循环不会再次启动程序Python Tkinter程序运行函数,但窗口在结束后弹出,python,user-interface,tkinter,Python,User Interface,Tkinter,好吧,我对tkinter还很陌生,我不能解决我现在遇到的这个问题 当我运行程序时,它运行函数,在结束后,弹出带有照片的窗口,但循环不会再次启动程序 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): # `com
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()
将tkinter作为tk导入
从tkinter进口*
从PIL导入ImageTk,图像
从tkinter导入消息框
进口YuaChanMainFunc
导入时间
单击时的def(事件=无):
#`command=`不带参数调用函数
#`bind`用一个参数调用函数
打印(“嘿,Yua!”)
YuaChanMainFunc.statement=“嘿,yua”
类窗口(框架):
def uuu init uuu(self,master=None):
超级()。\uuuu初始化\uuuuu(主)
self.master=master
self.pack()
硕士头衔(“Yua chan AI”)
self.img=ImageTk.PhotoImage(Image.open(“YuaChanAI/Yua Chan Artwork/YuaChan2.png”))
MainLB=tk.Label(master,image=self.img)
主绑定(“”,单击时)
MainLB.pack()
b=tk.Button(root,text=“Close”,command=root.destroy)
b、 包()
#YuaChanMainFunc.YuaChanAIMainFunc()
root=tk.tk()
#类的实例
app=窗口(根)
根目录。可调整大小(0,0)
根部几何形状(“310x500+1600+510”)
YuaChanMainFunc.YuaChanAIMainFunc()
#运行应用程序直到关闭
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()