calling a python script.exe on click of a button - python

I have a test.exe written in Python and I want to call it from this location C:\Folder. I know calling .py is possible like the code below:
Code:
import sys
import os
from tkinter import *
window=Tk()
window.title("Running Python Script")
window.geometry('550x200')
def run():
os.system('opencv_video.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=run)
btn.grid(column=0, row=0)
window.mainloop()
Is there a similar code that calls the test.exe that I have? Any suggestion will be highly appreciated. Thank you very much

Related

Trying to run .ahk files in tkinter

this is my first time here so if I break some unwritten rules please don't shoot me
I've been trying build an app for editing, reading and opening .ahk files. The last one gives me problems.
this code kind of does what I want except it fires the command immediately after opening the app.
from tkinter import *
from tkinter import filedialog
import os
import threading
root = Tk()
def openfile():
os.system( r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk')
def func():
print("hello")
btn1 = Button(text='programma', command=threading.Thread(target=openfile).start())
btn2 = Button(text='ander ding', command=func)
btn1.pack()
btn2.pack()
root.mainloop()
Any help would be much appreciated!
Change the following line (it just assigns the result of threading.Thread(...) to command option):
Button(text="run program1", command=threading.Thread(target=lambda: os.system(r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk').start()))
to
Button(text="run program1", command=lambda: threading.Thread(target=lambda: os.system(r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk')).start())
or
Button(text="run program1", command=lambda: threading.Thread(target=os.system, args=[r'\Users\merijn\PycharmProjects\infoprojectP3\venv\Scripts\ahkScript1.ahk']).start())
Same for buttons of run program2 and run program3.
Update: based on your updated code, you need to change the following line:
btn1 = Button(text='programma', command=threading.Thread(target=openfile).start())
to
btn1 = Button(text='programma', command=lambda: threading.Thread(target=openfile).start())

How do you execute another file when a button is pressed(tkinter) in Python?

I'm trying to make a launcher for another program but I just started with Python so I made a button, but I struggle to figure out how to execute another .py file. Any help?
When the button is pressed it activates the open_file() function and os opens the .py script.
from tkinter import *
import os
def open_file():
os.system('python file path here')
root=Tk()
btn = Button(root, text='Open .PY File', command=open_file)
btn.pack()
root.mainloop()
Here is a solution using from subprocess import call. All you have to do is replace 'YOUR_FILE_NAME' with... your file name :D
from tkinter import *
from subprocess import call
root=Tk()
root.geometry('200x100')
frame = Frame(root)
frame.pack(pady=20,padx=20)
def Open():
call(["python", "YOUR-FILE-NAME.py"])
btn=Button(frame,text='Open File',command=Open)
btn.pack()
root.mainloop()
What it will look like:
I hope this works for you :D

How to make Tkinter run a command using a button and label out my output in Terminal

So I am making a project that outputs responses from Google Sheets that asks me and gives me video ideas. I just need help figuring out how to make Tkinter output the whole response into a window.
Here is the main code for the project I am working on:
import pygsheets
import random
import tkinter
import numpy as np
gc = pygsheets.authorize()
sh = gc.open('Give Me Video Ideas (Responses)')
wks = sh.sheet1
for row in wks:
print(list(row))
And here is the Tkinter code I have so far:
import sys
import os
from tkinter import *
window=Tk()
window.title("Give me Video Ideas")
window.geometry('550x200')
def run():
os.system('python anothergithub.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=run)
btn.grid(column=0, row=0)
window.mainloop()
This is if you have variables in you deal line run(string)
import sys
import os
from tkinter import *
window=Tk()
window.title("Give me Video Ideas")
window.geometry('550x200')
def run():
os.system('python anothergithub.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=lambda: run("Hey"))
btn.grid(column=0, row=0)
window.mainloop()

Linking tkinter GUI with functions in a different module

I want to link GUI (1. modul) with the functions that are in a different module. Basically I need to link GUI with the program.
I created a very easy example:
modul1:
from modul2 import *
from tkinter import *
window = Tk()
window.title('Program')
window.geometry("300x300")
text_input= StringVar()
#result
result=Entry(window, textvariable=text_input)
result.place(x=6,y=15)
#Button
button=Button(window, text='X')
button.config(width=5, height=2, command=lambda: test())
button.place(x=10,y=70)
window.mainloop()
modul2:
import modul1
def test():
global text_input
text_input.set('hello')
EXPECTED:
This program should write "hello" into Entry window after clicking on a button.
RESULT: ERROR:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Ondrej\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Ondrej/Desktop/VUT FIT/IVS\modul1.py", line 17, in <lambda>
button.config(width=5, height=2, command=lambda: test())
NameError: name 'test' is not defined
Does anybody know where the problem is?
Thank you in advance for the help.
Yes, the problem you actually have is to do with a circular import. Modul1 imports Modul2 and Modul2 imports Modul1. a way you could solve this is to give the textinput as a parameter to the test function in modul2 like this:
modul1.py:
import modul2
from tkinter import *
window = Tk()
window.title('Program')
window.geometry("300x300")
text_input = StringVar()
# result
result = Entry(window, textvariable=text_input)
result.place(x=6, y=15)
# Button
button = Button(window, text='X')
button.config(width=5, height=2, command=lambda: modul2.test(text_input))
button.place(x=10, y=70)
window.mainloop()
modul2.py:
def test(text_input):
text_input.set('hello')
Unfortunately python really doesn't like you to do circular imports and this is good as this would actually mean an infinite loop. When would it stop importing the other file?
Edit: there would be a very convoluted way to make a class in a third module and set attributes on that, and then import this third module in both modul1 and modul2 to share variables between them but please don't...
Edit2: an example of this:
Modul1.py:
from shared import SharedClass
import modul2
from tkinter import *
window = Tk()
window.title('Program')
window.geometry("300x300")
SharedClass.text_input = StringVar()
# result
result = Entry(window, textvariable=SharedClass.text_input)
result.place(x=6, y=15)
# Button
button = Button(window, text='X')
button.config(width=5, height=2, command=lambda: modul2.test())
button.place(x=10, y=70)
window.mainloop()
Modul2.py:
from shared import SharedClass
def test():
SharedClass.text_input.set('hello')
shared.py
class SharedClass:
pass
This works due to the way python loads imported classes. They all are the same. This makes it so if you set properties on the class (not the instances of it) you can share the properties.

Simple Button Hide on Click

Hello I'm obviously not too experienced with tkinter very much and I couldnt find anything on what I was looking for, maybe someone could help me
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!" command=hide(d))
d.pack()
I would like it so that on click the command runs but the button is not defined when calling the command
You can not use anything if you are still building. Must you use configure and lambda function:
from tkinter import *
def hide(x):
x.pack_forget()
root = Tk()
d=Button(root, text="Click to hide me!")
d.configure(command=lambda: hide(d))
d.pack()
root.mainloop()
First, define the button, then add the command with the config method.
from tkinter import *
root = Tk()
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!")
d.pack()
d.config(command=lambda: hide(d))
root.mainloop()

Categories