-1

I'm working on a code editor based on nano. I've decided to make GUI for it, and the GUI language I chose is Python. I'm trying to make a button that opens batch files on Tkinter. I did so by importing import subprocess. Now, I created a button that opens the batch file with subprocess: ttk.Button(frm, text="New Session", command=subprocess.run(['csession.bat']) ).grid(column=0, row=1)

Little problem is, when I launch the file, the subprocess action instantly gets executed instead of waiting for the user to click. I'm using VSCode and also debugging in VSCode (if that does any difference).

1 Answers1

0

you need to use lambda if you want to define the command this way. you can also create a function and set the command to run it.

import tkinter as tk
from tkinter import ttk
import subprocess

def runBat():
    subprocess.run(['foo.bat'])

root = tk.Tk()

ttk.Button(root,text="text",command= lambda: subprocess.run(['foo.bat'])).pack()
ttk.Button(root,text="text2",command=runBat).pack()
root.mainloop()