我正在为另一个python程序创建一个UI,它本质上只是一个项目的交互式组件。目标是拥有一个从按钮运行的python程序更新(打印语句)的特定标签。这是我在我的UI中编写的程序...
import tkinter as ttk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
app = ttk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = ttk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='normal')
ttk.Label(app, text='Ex', bg='gray', font=(
'Verdana', 15)).pack(side=ttk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
def ex_activation():
global pro
print("Running program!")
pro = subprocess.Popen("python programex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button = ttk.Button(app, bg='black', image=photo, width=120, height=120, command=ex_activation)
ex_stop_button = ttk.Button(app, bg='Gray', text='Stop Program', width=12, command=ex_stop, height=3)
ex_stop_button['font'] = myFont
app.title("Ex")
ex_activation_button.pack(side=ttk.TOP)
ex_stop_button.pack(side=ttk.LEFT)
# app.mainloop()
while True:
try:
app.update()
app.update_idletasks()
except KeyboardInterrupt:
pass如果任何人对如何在我的代码中实现This StackOverflow Post有一个想法,我将非常感谢他们的帮助和支持。
为acw1668编辑这些是我运行的一些测试,在Pycharm运行窗口中得到了一些奇怪的数字,而不是UI。
Running program!
3528
Stopping Program 3528 ... Please Wait!
monitor done
Running program!
144
Stopping Program 144 ... Please Wait!
monitor done
Running program!
14008
Stopping Program 14008 ... Please Wait!
monitor done
Running program!
21748
Stopping Program 21748 ... Please Wait!
monitor done发布于 2020-08-15 05:44:57
我使用了您的代码的简化版本。要在tkinter应用程序中获取打印状态,我想到的最简单的方法是使用第三个文件:在我的例子中,是log.txt。
daemon.py会将一些输出(自定义函数) log()到文本文件中,而不是打印出来。在tkinter主循环中,应用程序将不断检查该文件的更改。
请注意,对于file.readline(),我只读了一行,因为tkinter标签更喜欢这样。如果您计划使用更多行,则可以将标签更改为文本区域。
无论如何:这是代码。我希望它能在你的项目中工作!
# FILE 1: daemon.py
from time import sleep
from sys import exit
def log(s): # This will be the equivalent to the print function
with open("log.txt", "w") as fileout:
fileout.write(s)
x = 0
while True:
try:
log("seconds elapsed: " + str(x))
x+=1
sleep(1)
except KeyboardInterrupt:
log("Stopping with x =" + str(x))
exit()# FILE 2: log.txt
# You can leave this file empty, but you must create a file named 'log.txt'.# FILE 3: mainapp.py
import tkinter as tk
import subprocess
import os
def ex_activation():
print("Running")
global pro
pro = subprocess.Popen("python daemon.py", shell=True)
def ex_stop():
print("Stopped") # I just dont know why, but this print can't be removed.
global pro
os.kill(pro.pid, 0)
app = tk.Tk()
ex_activation_button = tk.Button(app, text="run", command=ex_activation)
ex_stop_button = tk.Button(app, text="stop", command=ex_stop)
lab = tk.Label(app, text="If you see this, an error occurred")
with open("log.txt", "w+") as fileinout:
fileinout.write("Click run to start the execution!")
lab.config(text=fileinout.readline())
ex_activation_button.pack()
ex_stop_button.pack()
lab.pack()
# app mainloop
while True:
try:
app.update()
app.update_idletasks()
with open( "log.txt", "r" ) as filein:
lab.config(text=filein.readline())
except KeyboardInterrupt:
passhttps://stackoverflow.com/questions/63419891
复制相似问题