簡體   English   中英

從另一個程序啟動python程序

[英]Launch python program from another program

我正在為我的計算機科學A級課程制作一個庫存控制系統。 我的問題是我不知道如何在Button1之后使python啟動另一個python程序。

from tkinter import *
top=Tk()

Button1= Button(top,text='UPDATE STOCK', width=40)
Button1.place(x=80, y=20)

mainloop()
from Tkinter import Tk, Button, mainloop
import subprocess

def ext_python_script(event):
    subprocess.call(["python2.7", "sss.py"])   

if __name__ == '__main__':
    top = Tk()

    Button1 = Button(top, text='UPDATE STOCK', width=20, height=10)
    Button1.place(x=10, y=20)
    Button1.bind("<Button-1>", ext_python_script)

mainloop()

我們可以在這里使用bind。

import os
from tkinter import*
import subprocess

def otherlaunch():
    subprocess.call(['python.exe', "filename.py"]) # filename.py is the file

top=Tk()

top.title('Stock Control')
top.geometry('400x200')

Button1= Button(top,text='UPDATE STOCK', width=40,command=otherlaunch)
Button1.place(x=80, y=20)


mainloop()

您可以使用import語句從另一個程序中加載一個python程序,並使用command參數在單擊按鈕時執行某些操作,如下所示:

import os 
from tkinter import*
top=Tk()

top.title('Stock Control')
top.geometry('400x200')

def run_other():
    import filename  #notice that it is not a string, and there is no '.py'

Button1= Button(top,text='UPDATE STOCK', width=40, command=run_other)
Button1.place(x=80, y=20)


mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM