如何使用tkinter合并两个python代码

2024-06-17 12:38:27 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用tkinter在一个程序中使用两个python代码,当按下第一个按钮时它运行我的第一个项目当按下第二个按钮时它运行我的第二个项目


Tags: 项目代码程序tkinter按钮
2条回答

替换项目文件的路径:

import tkinter as tk
from tkinter import ttk
import os

win=tk.Tk()

def run1():
    os.system("python path to 1.py")
def run2():
    os.system("python path to 2.py")

button1=ttk.Button(win,text="Run 1.py",command=run1)
button2=ttk.Button(win,text="Run 2.py",command=run2)
button1.pack()
button2.pack()

win.mainloop()

1.2:

print("Hello ! from 1.py")

2.2:

print("Hello ! from 2.py")

我相信你需要按一个按钮同时运行两个程序,对吗

创建两个单独的程序。 最后的代码是:

from tkinter import Tk
from tkinter.ttk import Button
import os

root = Tk() # Creating the main window
root.title("Whatever you want")
root.geometry("300x200")

def open_files():    # function to run the apps/programs/files
    os.startfile("file1.py")
    os.startfile("file2.py")

open_b = Button(root, text="Open files", command=open_files)    # Creating the button
open_b.pack(pady=20)

root.mainloop()

相关问题 更多 >