Reputation: 59
I want to execute 2 function in the same time, (im new in python, and development in general) i tried to create a minecraft server setup assistant :
(the code is in development too)
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()
But when I execute the script , the second thread start only when i close the tkinter window.
edit: now the function are called correctly Could you help me ?
Upvotes: 0
Views: 90
Reputation: 178179
Thread
is not being called correctly. You are calling root.mainloop()
and passing its return value to the threads as target
, requiring it to run to completion in order to return. Instead, pass the function object itself as target by removing the parentheses. The second thread has the same issue, but needs to pass arguments as a separate args
tuple.
Use this:
threading.Thread(target=root.mainloop).start() # start the tkinter interface
threading.Thread(target=startndstop,args=("start",)).start() # start the minecraft server
Upvotes: 2