[英]trying to Browse processes with gui using tkinter
我正在尝试制作一个程序,我需要将两个过程链接在一起。 如果其中一个停止了,另一个也停止了,并且由于某种原因,我的图形用户界面在尝试浏览进程并检查我为此设置的条件时滞后。这是我的问题的短视频,请在此处输入链接描述。
import tkinter as tk
from tkinter import messagebox
import psutil
import os
class myapp():
def __init__(self):
self.win = tk.Tk()
self.win.title("test")
self.win.geometry('200x100')
self.win.config(bg='black')
self.win.resizable(False,False)
self.ctr = 0
self.tk_var = tk.StringVar()
self.tk_var.set("0")
lab=tk.Label(self.win, textvariable=self.tk_var,
bg='black', fg='#FF0000')
lab.place(x=90, y=45)
btn = tk.Button(self.win,text='test',command='',
bd=0,bg='#7E1600',width=10,
activebackground='#6D0000')
btn.pack(pady=20)
self.upd()
self.test()
self.win.mainloop()
def upd(self):
self.ctr += 1
self.tk_var.set(str(self.ctr))
self.win.after(50,self.upd)
def test(self):
self.service = "notepad.exe" in (i.name() for i in psutil.process_iter())
if self.service != True :
er = messagebox.showerror(title='error', message='notepad.exe has been stopped')
if er == 'ok':
self.win.destroy()
else:
self.win.after(500,self.test)
os.popen('notepad')
myapp()
我还在努力学习我的英语。
import tkinter as tk
from tkinter import messagebox
import psutil
import os
class myapp():
def __init__(self):
self.win = tk.Tk()
self.win.title("test")
self.win.geometry('200x100')
self.win.config(bg='black')
self.win.resizable(False, False)
self.ctr = 0
self.tk_var = tk.StringVar()
self.tk_var.set("0")
lab = tk.Label(self.win, textvariable=self.tk_var,
bg='black', fg='#FF0000')
lab.place(x=90, y=45)
btn = tk.Button(self.win, text='test', command='',
bd=0, bg='#7E1600', width=10,
activebackground='#6D0000')
btn.pack(pady=20)
self.upd()
self.win.mainloop()
def upd(self):
self.ctr += 1
self.tk_var.set(str(self.ctr))
self.win.after(50, self.test)
def test(self):
self.service = "notepad.exe" in (i.name() for i in psutil.process_iter())
if self.service != True:
er = messagebox.showerror(title='error', message='notepad.exe has been stopped')
if er == 'ok':
self.win.destroy()
self.win.after(500, self.upd)
os.popen('notepad')
myapp()
upd->test->upd->test... 只需调用一次upd。 无需调用测试。 还有,后面的function调用是错误的。 查看我修改后的代码。 很难用我简短的英语解释。
为了更好地理解,我在示例中重命名了一些函数...
我看到你的检查速度太快,这可能会导致低端计算机的延迟,另外,请记住,当你在初始化 class 时调用“check_notepad”function 时,每当你单击“测试”按钮时,它都会添加一个新的检查,也就是说,另一个检查循环开始起作用,这种级联使您的应用程序在 memory 中变得越来越重......
我的解决方案,除了对代码进行一些改进外,只是启动检查器一次,以免创建级联检查,如果您需要使用复选框而不是按钮可以做更多的意义......
import os
import tkinter as tk
from tkinter import messagebox
import psutil
class myapp(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# styles
self.geometry('200x100')
# vars
self.timer = 500
# widgets
self.tk_var = tk.IntVar(value=0)
lab = tk.Label(self, textvariable=self.tk_var)
lab.place(x=90, y=45)
self.check_var = tk.BooleanVar()
btn = tk.Checkbutton(self, text='Test', variable=self.check_var)
btn.pack(pady=20)
# Starts
self.update_label()
self.check_notepad()
def update_label(self):
self.tk_var.set(self.tk_var.get() + 1)
self.after(self.timer, self.update_label)
def check_notepad(self):
if self.check_var.get():
if not notepad_is_open():
er = messagebox.showerror(title='error',
message='notepad.exe has been stopped')
if er == 'ok':
self.destroy()
# call back loop
self.after(self.timer, self.check_notepad)
def notepad_is_open():
service = "notepad.exe" in (i.name() for i in psutil.process_iter())
return service
if __name__ == '__main__':
if not notepad_is_open():
os.popen('notepad')
myapp = myapp()
myapp.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.