我对python和tkinter相当陌生。我正在构建一个GUI来管道一个使用多个外部。exe程序的进程,我希望能够通知用户其中一个。exe进程已经启动或停止,因为它们可能需要很长时间,我希望用户能够知道它目前在进程中的位置。
使用我当前的代码,文本框更新在所有内容结束时立即发生,而不是在每个步骤之后。是否有一种方法可以在每个步骤中进行此更新,或者有我不知道的更好的方法?我对打开命令行并打印的可能性持开放态度。
import tkinter as tk
import subprocess as s
root = tk.Tk()
root.minsize(650,250)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
container = tk.Frame(root).grid(row=0, column=0)
exe_1_path = tk.StringVar(root, value = 'C:\Users\Process_data_step_1.exe')
exe_2_path = tk.StringVar(root, value = 'C:\Users\Process_data_step_2.exe')
exe_3_path = tk.StringVar(root, value = 'C:\Users\Process_data_step_3.exe')
config_file_path = tk.StringVar(root, value = 'C:\Users\Config.txt')
output_path = tk.StringVar(root, value = 'C:\Users\Output')
def start_processing():
# notifu user that we are starting to process the data
output_window.insert(tk.END, 'Starting to process data for exe1.n')
# I want the textbox to update here
s.check_call([exe_1_path.get(), '-c', config_file_path.get(), 'o', output_path.get])
output_window.insert(tk.END, 'Finished processing data for exe1.n')
# I want the textbox to update and here
output_window.insert(tk.END, 'Starting to process data for exe2.n')
# I want the textbox to update and here
s.check_call([exe_2_path.get(), '-c', config_file_path.get(), 'o', output_path.get])
output_window.insert(tk.END, 'Finished processing data for exe2.n')
# I want the textbox to update and here
output_window.insert(tk.END, 'Starting to process data for exe3.n')
# I want the textbox to update and here
s.check_call([exe_3_path.get(), '-c', config_file_path.get(), 'o', output_path.get])
output_window.insert(tk.END, 'Finished processing data for exe3.n')
# I want the textbox to update and here
start_button = tk.Button(container, text='Start', width=10, command = start_processing)
start_button.grid(row = 0, pady=10, padx=20)
output_window = tk.Text(root, height = 10, width = 75, bg = 'black', fg = 'white')
output_window.grid(row = 1, pady=10, padx=20)
root.mainloop()
使用output_window.update()
更新output_window中的文本例子:
import tkinter as tk
import subprocess as s
root = tk.Tk()
root.minsize(650,250)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
container = tk.Frame(root).grid(row=0, column=0)
exe_1_path = tk.StringVar(root, value = 'C:\Users\Process_data_step_1.exe')
exe_2_path = tk.StringVar(root, value = 'C:\Users\Process_data_step_2.exe')
exe_3_path = tk.StringVar(root, value = 'C:\Users\Process_data_step_3.exe')
config_file_path = tk.StringVar(root, value = 'C:\Users\Config.txt')
output_path = tk.StringVar(root, value = 'C:\Users\Output')
def start_processing():
# notifu user that we are starting to process the data
output_window.insert(tk.END, 'Starting to process data for exe1.n')
# I want the textbox to update here
s.check_call('python example_subprocess1.py')
#s.check_call([exe_1_path.get(), '-c', config_file_path.get(), 'o', output_path.get])
output_window.insert(tk.END, 'Finished processing data for exe1.n')
output_window.update()
# I want the textbox to update and here
output_window.insert(tk.END, 'Starting to process data for exe2.n')
# I want the textbox to update and here
s.check_call('python example_subprocess1.py')
#s.check_call([exe_2_path.get(), '-c', config_file_path.get(), 'o', output_path.get])
output_window.insert(tk.END, 'Finished processing data for exe2.n')
output_window.update()
# I want the textbox to update and here
output_window.insert(tk.END, 'Starting to process data for exe3.n')
# I want the textbox to update and here
s.check_call('python example_subprocess1.py')
#s.check_call([exe_3_path.get(), '-c', config_file_path.get(), 'o', output_path.get])
output_window.insert(tk.END, 'Finished processing data for exe3.n')
output_window.update()
# I want the textbox to update and here
start_button = tk.Button(container, text='Start', width=10, command = start_processing)
start_button.grid(row = 0, pady=10, padx=20)
output_window = tk.Text(root, height = 10, width = 75, bg = 'black', fg = 'white')
output_window.grid(row = 1, pady=10, padx=20)
root.mainloop()
其中示例子过程(对您来说已经微不足道,但对其他人来说&以后使用):
import tkinter
from tkinter import *
import numpy as np
from tkinter import messagebox
import time
#luodaan kuva...
ikkuna=tkinter.Tk()
ikkuna.title("Example subprocess 1 (here some serial data processing...)")
ikkuna.geometry("800x400")
kanvaasi=Canvas(ikkuna,width=800,height=400)
kanvaasi.pack(fill="both",expand=True)
kanvaasi.create_text(370,80,text="V1")
kanvaasi.create_text(370,130,text="V2")
kanvaasi.create_text(370,180,text="V3")
kanvaasi.create_text(370,220,text="VPack")
kanvaasi.create_text(370,270,text="Capacity")
def read():
global v1,v2,v3,vpack,capacity
global kv1,kv2,kv3,kv4,kv5
#this is only simulation, in real case replace the simulation code with the real one...
v1=np.random.random(1)[0].round(decimals=2)
v2=np.random.random(1)[0].round(decimals=2)
v3=np.random.random(1)[0].round(decimals=2)
vpack=np.random.random(1)[0].round(decimals=2)
capacity=np.random.random(1)[0].round(decimals=2)
#...end of simulation
try:
kanvaasi.delete(kv1)
kanvaasi.delete(kv2)
kanvaasi.delete(kv3)
kanvaasi.delete(kv4)
kanvaasi.delete(kv5)
except:
pass
#let's update the tkinter accordingly...
kv1=kanvaasi.create_text(430,80,text=v1)
kv2=kanvaasi.create_text(430,130,text=v2)
kv3=kanvaasi.create_text(430,180,text=v3)
kv4=kanvaasi.create_text(430,220,text=vpack)
kv5=kanvaasi.create_text(430,270,text=capacity)
ikkuna.destroy()
painike1=Button(ikkuna,text="Read",command=read,height=14,width=20)
painike1_ikkuna=kanvaasi.create_window(120,70,anchor="nw",window=painike1)
ikkuna.mainloop()