【发布时间】:2020-10-30 20:25:12
【问题描述】:
我想同时执行两个函数, (我是 python 新手,一般是开发) 我试图创建一个我的世界服务器设置助手:
(代码也在开发中)
import os
import tkinter as tk
from tkinter import ttk
import threading
import wget
import json
from pathlib import Path
...
def startndstop(x):
os.chdir(newpath)
if x == "start":
start = str("java" + ram() + namejar + " nogui")
print("Demarrage du serveur", name, "!")
os.system(str(start))
elif x == "stop":
os.system("stop")
root = tk.Tk()
root.title("Yann Installer")
root.iconbitmap("./minecraft_icon_132202.ico")
root.geometry("640x390")
root.resizable(width=False, height=False)
root.configure(bg="#0f800f")
bgimage = tk.PhotoImage(file="background.gif"))
l = tk.Label(root, image=bgimage)
l.pack()
installbutton = tk.Button(root, text="Installer", command=lambda :threading.Thread(target=install("1.8.3")).start())
installbutton.lift(aboveThis=l)
installbutton.place(x=10, y=10)
startbutton = tk.Button(root, text="Demarrer", command=lambda :threading.Thread(target=startndstop("start")).start())
startbutton.lift(aboveThis=l)
startbutton.place(x=10, y=40)
listeVersion = ttk.Combobox(root, values=versionlist())
listeVersion.current(0)
listeVersion.lift(aboveThis=l)
listeVersion.place(x=80, y=10, width=100)
threading.Thread(target=root.mainloop).start()
threading.Thread(target=startndstop,args=('start',)).start()
但是当我执行脚本时,第二个线程只有在我关闭 tkinter 窗口时才会启动。
编辑:现在函数被正确调用 你能帮帮我吗?
【问题讨论】:
-
这里没有足够的东西来测试你的问题。
-
@Mike-SMT 我添加了部分代码
-
你不应该在子线程中运行
tkinter.mainloop()。只需为startndstop()创建线程,然后运行tkinter.mainloop()。
标签: python multithreading tkinter