[英]Creating a UI for another Python program. How do I make a printing label in tkinter for Python 3?
I am creating a UI for another python program and it essentially just makes an interactive component to the project.我正在为另一个 python 程序创建一个 UI,它实际上只是为项目创建了一个交互式组件。 The goal is to have a specific label that updates (print statements) from the python program the button runs.
目标是有一个特定的 label 从按钮运行的 python 程序更新(打印语句)。 This is what I have programmed in my UI...
这是我在我的 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
If anyone has an idea of how I can implement This StackOverflow Post into my code, I would greatly appreciate the help and support.如果有人知道如何在我的代码中实现This StackOverflow Post ,我将非常感谢您的帮助和支持。
EDIT for acw1668 These are some tests I had run and got some strange numbers in the Pycharm run window instead of the UI.编辑 acw1668 这些是我运行的一些测试,并在 Pycharm 运行 window 而不是 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
I used a simplified version of your code.我使用了您的代码的简化版本。 To get the print status inside the tkinter app, the easiest way I have thinked of is using a third file: in my case,
log.txt
.要在 tkinter 应用程序中获取打印状态,我想到的最简单的方法是使用第三个文件:就我而言,
log.txt
。
The daemon.py
will log()
some output (custom function) to the text file instead of printing it. daemon.py
将log()
一些 output (自定义函数)到文本文件而不是打印它。 And in the tkinter mainloop, the app will be constantly checking changes to that file.在 tkinter 主循环中,应用程序将不断检查对该文件的更改。
Note that with file.readline()
, I am just reading one line, as tkinter Labels prefer that.请注意,使用
file.readline()
时,我只阅读一行,因为 tkinter 标签更喜欢这样。 If you plan using more lines, you could change the label to a textarea.如果您计划使用更多行,您可以将 label 更改为 textarea。
Anyway: here is the code.无论如何:这是代码。 I hope it'll work inside your project!
我希望它会在你的项目中工作!
# 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:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.