简体   繁体   English

将 function output 链接到 python 中的 GUI

[英]Linking function output to GUI in python

Hi I am trying to write a simple program that while working in the background looks for how long a specific executable is working.您好我正在尝试编写一个简单的程序,在后台工作时查找特定可执行文件的工作时间。 This bit works in the shell of the IDE however I don't know how to connect the shells output to the GUI so that it refreshes and shows the time in seconds that a program is working for.此位在 IDE 的 shell 中有效,但是我不知道如何将外壳 output 连接到 GUI,以便它在几秒钟内工作并显示该程序的刷新时间。

[I have tried StringVar but it failed] [我尝试过 StringVar 但失败了]

If someone would help it would be lovely如果有人会帮助它会很可爱

Below is the code which as stated works only partially:以下是如上所述仅部分有效的代码:

import time
from tkinter import * # Another GUI-Framework is PYQT5
import wmi
import sys

c = wmi.WMI()
Task_List = []


class AppButtons:
    # A variable and a List for the gatering of currently running processes

    def __init__(self, master):
        main_frame = Frame(master)
        main_frame.pack()

        '''
        #Program title
        self.Label = Label(main_frame, text='The time monitoring app')
        self.Label.config(font=("Courier", 30), anchor=CENTER)
        self.Label.grid(row=0, column=0)
        '''


        # Program start
        self.strat_timer = Button(main_frame, text='Beggin the app running time monitoring!!!', command=self.main_timer)
        self.strat_timer.config(font=("Courier", 12))
        self.strat_timer.grid(row=1, column=0,pady=10, sticky=E+W) # pady and padx add space btw the widgets in the x and y directions corespondingly

        # Program termination
        self.end_timer = Button(main_frame, text='Terminate the app timmer', command=self.timer_kill)
        self.end_timer.config(font=("Courier", 12))
        self.end_timer.grid(row=1, column=1,pady=10, sticky=E+W)

        # Output description
        self.time_overwatch_label1 = Label(main_frame, bg='red' ,width=60, height=1 , text='Program: WorldOfTanks.exe | Running time:')
        self.time_overwatch_label1.grid(row=2, column=0,)

        self.time_overwatch_label2 = Label(main_frame, bg='Yellow',width=60, height=1 , text='Program: chrome.exe | Running time:')
        self.time_overwatch_label2.grid(row=3, column=0)

        self.time_overwatch_label3 = Label(main_frame, bg='red',width=60, height=1 , text='Program: pycharm64.exe | Running time:')
        self.time_overwatch_label3.grid(row=4, column=0)


        # Output part
        self.output1 = Label(main_frame, bg='red' ,width=60, height=1, text=self.main_timer)
        self.output1.grid(row=2, column=1)

        self.output2 = Text(main_frame, bg='Yellow', width=60, height=1)
        self.output2.grid(row=3, column=1)

        self.output3 = Text(main_frame, bg='red', width=60, height=1)
        self.output3.grid(row=4, column=1)

    # gives the list of currently running processes using the WMI library
    def running_tasks(self):
        Task_List.clear()
        for process in c.Win32_Process():
            Task_List.append(process.Name)
        return Task_List

    def program_search(self):
        self.running_tasks()
        if any('WorldOfTanks.exe' in s for s in Task_List):
            return True
        else:
            return False

    # Main fuction which is responsible for the output of the timmer (in this example of WorldOfTanks.exe)
    # This fuction cosists of "running_tasks" and "task_search". It should be called from the GUI window
    def main_timer(self):
        start_time = time.time()
        while True:
            if self.program_search() is True:
                print('WoT is Running for', "--- %s seconds ---" % (time.time() - start_time))
                continue
            if time.time() - start_time < 21:
                print("World of Tanks wasn't running")
                break
            if self.program_search() is False:
                print('WorldOfTanks.exe was running for', "--- %s seconds ---" % (time.time() - start_time))
                break


    def timer_kill(self):
        sys.exit()


root = Tk()

# Title of the window is displayed on the uppermost bar
root.title("The Time Monitoring App")

# Initializes the class
b = AppButtons(root)

root.mainloop()

The most sensible approach would be to create three StringVar() for the three running times and linking them to the labels.最明智的方法是为三个运行时间创建三个StringVar()并将它们链接到标签。 At each iteration of the loop (which might be less demanding if you included a time.sleep(.5) to wait for half a second before running it again), update all of the string variables.在循环的每次迭代中(如果您包含time.sleep(.5)以等待半秒再运行它,这可能要求不高),更新所有字符串变量。

Creating and linking the string variables:创建和链接字符串变量:

    self.t1 = StringVar()
    self.t1.set("No time recorded for WoT") ## Set to no output before the button is pressed

    self.output1 = Label(main_frame, bg='red' ,width=60, height=1, textvariable=self.t1)
    self.output1.grid(row=2, column=1)

Updating their values:更新它们的值:

def main_timer(self):
    start_time = time.time()
    while True:
        time.sleep(0.5)
        if self.program_search() is True:
            self.t1.set('WoT is Running for', "--- %s seconds ---" % (time.time() - start_time))
            continue
        if time.time() - start_time < 21:
            self.t1.set("World of Tanks wasn't running")
            break
        if self.program_search() is False:
            self.t1.set('WorldOfTanks.exe was running for', "--- %s seconds ---" % (time.time() - start_time))
            break

Whenever you change the value of self.t1 in the method .main_timer() , the label will be updated as it is directly linked to this StringVar.每当您在.main_timer()方法中更改self.t1的值时,label 将被更新,因为它直接链接到此 StringVar。 Important : The text of the label has to be set using textvariable , not text , if you want it to change automatically.重要提示:label 的文本必须使用textvariable设置,而不是text ,如果您希望它自动更改。

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

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