简体   繁体   English

为另一个 Python 程序创建 UI。 如何在 tkinter 中为 Python 3 打印 label?

[英]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.pylog()一些 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.

相关问题 我正在为我的 python 程序创建一个 tkinter UI。 但是,我对 tk.button 有疑问。 它强调了我的图像按钮线 - I am creating a tkinter UI for my python program. However, I have an issue with the tk.button. It underlines my image button line 为 Python 程序创建 UI。 使用 tkinter 的 ttk 主题进行主题化,但它不适用于标签和按钮并返回一个奇怪的错误 - Creating a UI for a Python Program. Theming with ttk themes for tkinter but it doesn't work for labels and buttons and returns a strange error 我将文件(.jpg)保存在另一个文件夹中,我想在我的 python 程序中使用该文件。 我如何导入它们 - i save files(.jpg) in another folder and i want to use that files in my python program. how do i import them 如何使用 Tkinter 刷新 python 中的 label - How do i refresh the label in python with Tkinter 如何使用Python 3在Tkinter中将标签居中? - How do I center a label in Tkinter using Python 3? 如何在python中的tkinter中使用图像更新标签? - How do I update a label with an image in tkinter in python? 如何更正此函数以在Tkinter Python中创建标签? - How do I correct this function to create a label in Tkinter Python? 如何在 python tkinter 中循环 label 变量? - How do I loop the label variable in python tkinter? 如何根据条件删除python tkinter标签? - How do I delete a python tkinter label based on condition? Python tkinter - 更改由循环创建的Label,我该怎么办? - Python tkinter - Change the Label created by a loop, how can I do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM