how to link a button to another .py file in python2.7 - python

How do I link a python button?
Basically I want a python button to execute another python file.
I am using Tk.
I've created this button and would like it to execute the script
from Tkinter import *
import Tkinter as tk
root = tk.Tk()
b = Button(toolbar, text="Travelling", width=9 )
b.pack(side=LEFT, padx=2, pady=2)
mainloop()
How do I make this button execute another python script on click?

If you want execute another python script use execfile():
#-*- coding: utf-8 -*-
from Tkinter import *
master = Tk()
def callback():
execfile("script.py")
b = Button(master, text="OK", command=callback)
b.pack()
mainloop()

Related

calling a python script.exe on click of a button

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

Run another python file that shows camera preview using Tkinter window

I want run python file which opens camera preview by Tkinter button that placed on the main window.But it runs automatically camera window without opening the main window when executing the code.
from tkinter import *
import tkinter as tk
import os
window = tk.Tk()
window.title("Camera")
window.geometry("640x480")
lbl=Label(window,text="Start", font=("Arial Bold",10))
lbl.grid(column=0,row=0)
btn = Button(window, text="Start",command=os.system('capture.py'))
btn.grid(column=1, row=0)
window.mainloop()
It should be command=lambda: os.system('capture.py'):
btn = Button(window, text="Start",command=lambda: os.system('capture.py'))
btn.grid(column=1, row=0)
If you use () near a function name, it will call the function immediately. Which is not what you want, you want it to be called upon press, so just pass the function name(with lambda if it has arguments) and tkinter will handle the rest.

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

Trying to make a simple menu with tkinter but returns "Display is not capable of DPMS"

I am very noob and I need help please.
Here's my code:
from tkinter import *
root = Tk()
root.geometry('500x500')
def callback() :
print ("click!")
b = Button(root, text="OK", command=callback)
b.pack()
mainloop()
Try This:
from Tkinter import *
import tkMessageBox
root = Tk()
root.geometry('200x60')
root.title('Test Application')
def notify():
tkMessageBox.showwarning('Notification', 'Display is not capable of DPMS!')
b1 = Button(root, text="Run!", command=notify)
b1.pack()
root.mainloop()
Uri, you should probably run it on your computer instead of on the web. If you are using a chromebook go into settings and enable Linux.
——-Windows-——
Windows Install:
Click here
——-Mac-——
Mac Install:
click here
——-Linux-——
Linux Problems:
click here
Linux Install:
click here

tkinter button unable to open different python code

I am trying to run a .py file from another python code using the following sequence.
from tkinter import *
import os
import sys
def open_SS():
print('processing')
os.system('cd /home/pi/Desktop/Backup')
os.system('python p.py')
def close_window():
master.destroy()
master = Tk()
master.minsize(width=500, height=300)
Button(master, text='Exit', command=close_window).grid(row=12, column=6, sticky=W, pady=4)
Button(master, text='SS', command=open_SS).grid(row=12, column=8, sticky=W, pady=4)
mainloop( )
The Exit button does the command, but the 'SS' button does not, the word 'processing' does get printed, just the running of the p.py file. I tried running those two os.system commands on a command terminal and it works fine. p.py is supposed to input GPIO signals to a Raspberry Pi.
from tkinter import *
import os
import sys
master = Tk()
def open_SS():
print('processing')
os.system("python /home/pi/Desktop/Backup/p.py")
btn=Button(master,text="click here")
btn.grid(row=0,column=1)
btn.bind("<Button>",open_SS)

Categories