Problem with tkinter button

Hi I am new here,
I am having some issue with a simple code:
import tkinter as tk
r = tk.Tk()

def Opencalc():
import os
os.system(’“calc.exe”’)
r.title(‘Open calc’)
button = tk.Button(r, text=‘Open’, width=25, command=Opencalc, activebackground=“red”)
button.pack()

r.mainloop()

I am just learning python, when i click on the button, it open calc, but the button remain pressed why?
I want the button to be released after the calc is opened

1 Like

The button remains pressed until you close calc. That’s because the & symbol is missing in os.system(). I’ve tried this code on Linux, but should work on Windows too.

import tkinter as tk
import os

r = tk.Tk()

def Opencalc():
    os.system("kate&")

r.title('Open calc')

button = tk.Button(r, text='Open', width=25, command=Opencalc, activebackground="red")
button.pack()   

r.mainloop()
1 Like

Thanks for your answer, but still not working, I didn’t understand why you have put kate, but i tried with calc and still is not working with windows

1 Like

I don’t have calc.exe installed. The program hangs on os.system("calc.exe"), to check change it to print("calc.exe") and you’ll see it doesn’t hang on the button.

Try using subprocess and change to the calc.exe folder

import subprocess
subprocess.call(['C:\\Temp\\folder\\calc.exe'])
1 Like

Actually no, this wouldn’t work, cmd.exe does not have a “send to background” syntax like this. The subprocess version would probably not work either since it is blocking.

@Gagandeep What version of Windows are you using? Your code runs correctly on my Windows 10 machine, and the button correctly returned to unpressed state after calc is launched.

1 Like

Hi,
I am using windows 7 64bit

1 Like

no, is not working

1 Like

Hi all,
thanks for the help,
I sorted out doing this:
import subprocess
subprocess.Popen(‘C:\Windows\System32\calc.exe’)

1 Like

@Gagandeep thanks for your help, it worked on me too.