我有一个应用程序通过文本小部件显示进度消息,因此如果有很多行,它将滚动。它还会显示一个“任务”。每个步骤的进度条,以及不同的' Total'完成进度条。
我的情况是,如果他们没有“内容”,我希望不显示进度条。所以他们都没有在处理的开始和结束。 ' Total'一旦第一步完成,就会显示进度条,并且“任务”将会显示。如果任务足够长,可以显示进度条。
它是一个很棒的应用程序,但这里是我所拥有的缩短版本(但仍然很长)。
#!/usr/bin/python
# coding=utf-8
# Try to work with older version of Python
from __future__ import print_function
import sys
if sys.version_info.major < 3:
import Tkinter as tk
import Tkinter.ttk as ttk
else:
import tkinter as tk
import tkinter.ttk as ttk
#============================================================================
# MAIN CLASS
class Main(tk.Frame):
""" Main processing
"""
def __init__(self, root, *args, **kwargs):
tk.Frame.__init__(self, root, *args, **kwargs)
self.root = root
self.MW_f = tk.Frame(self.root)
# Progress Messages
self.PM_prog_msgs_lf_var = tk.StringVar()
self.PM_prog_msgs_lf = tk.LabelFrame(self.MW_f,
text=' Progress Messages: ',
relief='sunken')
# Progress Message text widget
self.PM_prog_msgs_max_frame_height = 6
self.PM_prog_msgs_line_count = 0
self.PM_prog_msgs_t = tk.Text(self.PM_prog_msgs_lf, height=self.PM_prog_msgs_max_frame_height, width=100)
self.PM_prog_msgs_t_vbar = ttk.Scrollbar(self.PM_prog_msgs_lf,
orient='vertical', command=self.PM_prog_msgs_t.yview)
self.PM_prog_msgs_t['yscrollcommand'] = self.PM_prog_msgs_t_vbar.set
self.PM_prog_msgs_t['state'] = 'disabled'
self.PM_prog_msgs_t['border'] = 3
self.PM_task_progbar_var = tk.IntVar()
self.PM_task_progbar = ttk.Progressbar(self.MW_f,
orient='horizontal',
mode='indeterminate',
variable=self.PM_task_progbar_var)
self.PM_progbar_var = tk.IntVar()
self.PM_progbar = ttk.Progressbar(self.MW_f,
orient='horizontal',
mode='determinate',
variable=self.PM_progbar_var,
maximum=4)
self.PM_go_btn = tk.Button(self.MW_f,
text='Go',
command=self.action_go_btn)
self.MW_stsbar_tvar = tk.StringVar()
self.MW_stsbar = tk.Label(self.MW_f,
textvariable=self.MW_stsbar_tvar)
# Grid the widgets
self.MW_f.grid()
MW_grid_row = 0
# Place into MW_f
self.PM_prog_msgs_lf.grid(row=MW_grid_row, column=0, sticky='we')
# Place into PM_prog_msgs_lf
self.PM_prog_msgs_t.grid(row=0, column=0)
self.PM_prog_msgs_t_vbar.grid(row=0, column=1, sticky='ns')
MW_grid_row += 1
self.PM_task_progbar_row = MW_grid_row
self.PM_task_progbar.grid(row=self.PM_task_progbar_row, sticky='we')
MW_grid_row += 1
self.PM_progbar_row = MW_grid_row
self.PM_progbar.grid(row=self.PM_progbar_row, sticky='we')
MW_grid_row += 1
self.MW_stsbar.grid(row=MW_grid_row, sticky='w')
MW_grid_row += 1
self.PM_go_btn.grid(row=MW_grid_row)
# Remove these until needed
self.PM_task_progbar.grid_remove()
self.PM_progbar.grid_remove()
self.PM_prog_msgs_t_vbar.grid_remove()
self.MW_dsp_stsbar_msg('Processing: Refer to progress message pane for more details')
# Window Displays Now
#=========================================================================================
# Functions used by window
def action_go_btn(self):
"""
"""
msg = 'Line 1\nLine 2'
self.PM_dsp_prog_msgs(msg, 2)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
self.PM_dsp_prog_msgs('Line 3', 1)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
msg = 'Line 4\nLine 5\nLine 6'
self.PM_dsp_prog_msgs(msg, 3)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
msg = 'Line 7\nLine 8\nLine 9'
self.PM_dsp_prog_msgs(msg, 3)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
self.PM_progbar.grid_remove()
return
def MW_dsp_stsbar_msg(self, msg):
"""
"""
# Display the statusbar
self.MW_stsbar_tvar.set(msg)
return
def PM_dsp_prog_msgs(self, msg, lines):
"""
"""
# Populate the message
self.PM_prog_msgs_t['state'] = 'normal'
self.PM_prog_msgs_t.insert(tk.END, ('\n'+msg))
self.PM_prog_msgs_t['state'] = 'disabled'
self.root.update_idletasks()
self.PM_prog_msgs_line_count += lines
# If its time display the vert scroll bar
if not self.PM_prog_msgs_t_vbar.winfo_viewable():
if self.PM_prog_msgs_line_count > self.PM_prog_msgs_max_frame_height:
self.PM_prog_msgs_t_vbar.grid(row=0, column=1)
self.root.update_idletasks()
# Show the last line.
self.PM_prog_msgs_t.see(tk.END)
self.root.update_idletasks()
return
def PM_progbar_update(self):
"""
"""
if not self.PM_progbar.winfo_viewable():
self.PM_progbar.grid()
# Remove the task progbar
self.PM_task_progbar.stop()
self.PM_task_progbar.grid_remove()
# Increment the progbar
self.PM_progbar.step()
self.root.update_idletasks()
return
def PM_task_progbar_update(self):
"""
"""
# Display if not already displayed
if not self.PM_task_progbar.winfo_viewable():
self.PM_task_progbar.grid()
self.root.update_idletasks()
# Step
self.PM_task_progbar.start()
self.root.update_idletasks()
return
# MAIN (MAIN) ===========================================================================
def main():
""" Run the app
"""
# # Create the screen instance and name it
root = tk.Tk()
app = Main(root)
root.mainloop()
root.quit()
# MAIN (STARTUP) ====================================================
# This next line runs the app as a standalone app
if __name__ == '__main__':
# Run the function name main()
main()
当&#39;任务&#39;进度条显示“进度消息”&#39;标签框架是重复的,并向下移动一行(参见屏幕截图)
抱歉,我无法附上截图。这是截图的文字版本!
- 进度讯息---------------------------------------
进度讯息---------------------------------------
消息第1步
[任务进度条]
[总进度条]
显示屏保持这种状态直到结束。即使在“任务”中也是如此。进度条被删除,重复仍然存在。当Total&#39; Total&#39;总计时,显示屏不会添加第三个版本。显示进度条。
当两个进度条被移除时,一切都会在最后纠正。
所有小部件都网格化到正确的框架,行/列似乎是正确的,所以我在哪里寻找纠正这个。
非常感谢,对不起,这已经很久了。
MacBookPro(2007),python 3.4,tkinter 8.5