繁体   English   中英

我可以导入 python 脚本以使其在我的 GUI 中运行吗?

[英]Can I import a python script to make it run in my GUI?

我正在开发一个 GUI 应用程序,希望用来在其中运行 python 脚本(甚至不知道是否可能),但我不太明白如何将脚本导入我的 GUI 代码以供使用,比如如果我点击一个按钮,我可以让脚本运行吗?

图形用户界面:

import tkinter as tk
from tkinter import Button, Canvas, Image, PhotoImage, filedialog, Text
import os
from tkinter.constants import COMMAND, DISABLED

root = tk.Tk()

canvas = tk.Canvas(root, height = 750, width = 800, bg = "#110a60")
canvas.pack()

root.resizable(False, False)
 
frame = tk.Frame(root, bg = "black")
frame.place(relheight = 0.8, relwidth = 0.8, relx = 0.1, rely = 0.1)

start_button = Button(root, text = 'Start Program', padx = 10, pady = 5, fg = "#FFFF00", bg = "#012456", state = DISABLED)
start_button.place(x = 350, y = 680)

exit_button = Button(root, text = 'Exit', padx = 20, pady = 5, fg = "#FFFF00", bg = "#012456", command = exit)
exit_button.place(x = 368, y = 714.5 )

root.mainloop()

假设脚本被称为 program.py 我可以导入它并使其在 canvas 中运行当我单击启动程序按钮时

import program

我试过这样做:

import tkinter as tk
from tkinter import Button, Canvas, Image, PhotoImage, filedialog, Text
import os
from tkinter.constants import COMMAND, DISABLED

def script():
    os.startfile('[path]program.py')

root = tk.Tk()

canvas = tk.Canvas(root, height = 750, width = 800, bg = "#110a60")
canvas.pack()

root.resizable(False, False)

frame = tk.Frame(root, bg = "black")
frame.place(relheight = 0.8, relwidth = 0.8, relx = 0.1, rely = 0.1)

start_button = Button(root, text = 'Start Program', padx = 10, pady = 5, fg = "#FFFF00", bg = "#012456", command = script)
start_button.place(x = 350, y = 680)

exit_button = Button(root, text = 'Exit', padx = 20, pady = 5, fg = "#FFFF00", bg = "#012456", command = exit)
exit_button.place(x = 368, y = 714.5 )

root.mainloop()

But this only runs the script in another window, I want it to run inside the canvas I made in other means I want the text outputting from the script to be printed inside the canvas and every user input to be made inside the canvas

所以我的脚本所做的一切都显示在我在 canvas 中制作的 GUI 中

在 header 中导入脚本(与主文件在同一目录中的 program.py)

from program import function

然后只需调用 function 作为 tkinter 按钮命令

start_button = Button(root, text = 'Start Program', padx = 10, pady = 5, fg = "#FFFF00", bg = "#012456", command = function)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM