[英]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.