如何使用 python 和 glade 创建进度条以将日志文件从一个系统复制到另一个系统?
How to create progressbar using python and glade for copying log files from one system to another system?
我正在使用这个 bash 脚本将日志文件从一个系统复制到另一个系统
#!/bin/bash s="/var/log"
d="/root/Directory" BACKUPFILE=scripts.backup.`date +%F`.tar.gz
scp -r root@:$s rsync -chavzP --stats root@ipaddr
filename=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar -czvf /$BACKUPFILE $s
tar --create --gzip --file=$d$filename $s
rm -rf /root/aaa/log
我也做过这样的进度条代码
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class ProgressBarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ProgressBar Demo")
self.set_border_width(10)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.progressbar = Gtk.ProgressBar()
vbox.pack_start(self.progressbar, True, True, 0)
button = Gtk.CheckButton("Show text")
button.connect("toggled", self.on_show_text_toggled)
vbox.pack_start(button, True, True, 0)
button = Gtk.CheckButton("Activity mode")
button.connect("toggled", self.on_activity_mode_toggled)
vbox.pack_start(button, True, True, 0)
button = Gtk.CheckButton("Right to Left")
button.connect("toggled", self.on_right_to_left_toggled)
vbox.pack_start(button, True, True, 0)
self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
self.activity_mode = False
def on_show_text_toggled(self, button):
show_text = button.get_active()
if show_text:
text = "some text"
else:
text = None
self.progressbar.set_text(text)
self.progressbar.set_show_text(show_text)
def on_activity_mode_toggled(self, button):
self.activity_mode = button.get_active()
if self.activity_mode:
self.progressbar.pulse()
else:
self.progressbar.set_fraction(0.0)
def on_right_to_left_toggled(self, button):
value = button.get_active()
self.progressbar.set_inverted(value)
def on_timeout(self, user_data):
"""
Update value on the progress bar
"""
if self.activity_mode:
self.progressbar.pulse()
else:
new_value = self.progressbar.get_fraction() + 0.01
if new_value > 1:
new_value = 0
self.progressbar.set_fraction(new_value)
# As this is a timeout function, return True so that it
# continues to get called
return True
win = ProgressBarWindow() win.connect("delete-event", Gtk.main_quit)
win.show_all() Gtk.main()
但我不知道如何将我的脚本嵌入到这个进度条代码中。
有一些解决这个问题的方法,但你必须关心的主要问题是 GUI 和耗时任务(长任务)不是好朋友。大多数 GUI 框架使用它们的主循环来处理用户输入处理并绘制 UI。话虽这么说,你必须将那些长任务与主要任务分开 UI 并且有一些方法可以做到这一点,无论是线程,异步方法等,它都会恢复到你选择的语言如何处理这些问题.
然后就是如何调用要跟踪的OS函数的问题。可能最好的方法是以编程方式在您的代码中实现它们,但这将是一项耗时的工作。因此,使用 shell 脚本将是您的选择,这样做会引出一个问题:如何获取这些命令的输出?嗯,有 popen,因为您将使用 python,那么您必须使用 popen 调用生成 shell 脚本。最好的方法似乎是 subprocess。另一个改进是更好地定制 bash 脚本,该脚本可以获得一些先前对命令结果(成功或失败)和 conditioned/formatted 输出的分析。
希望你能明白我要去哪里...你必须解析通常进入控制台的数据,解释它并相应地更新 UI。
我给你一个 Gtk Window 的简单例子,它有一个进度条,它会随着 shell 命令输出的每一行而跳动 (tree /
):
import time
import threading
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk, GObject
def app_main():
win = Gtk.Window(default_height=50, default_width=300)
win.connect("delete-event", Gtk.main_quit)
win.connect("destroy", Gtk.main_quit)
progress = Gtk.ProgressBar(show_text=True)
win.add(progress)
def update_progress(i):
progress.pulse ()
#progress.set_fraction (i/100.0) # use this for percentage
#progress.set_text(str(i))
return False
def example_target():
i = 0 # can be used to pass percentage
proc = subprocess.Popen(['tree', '/'],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line != '':
time.sleep(0.1) # output is too quick, slow down a little :)
GLib.idle_add(update_progress, i)
else:
break
win.show_all()
thread = threading.Thread(target=example_target)
thread.daemon = True
thread.start()
if __name__ == "__main__":
app_main()
Gtk.main()
在上面的例子中,我们使用了线程。您可以在控制台上尝试的 shell 命令会在您的磁盘上转储文件夹结构树。可以是任何东西,但目标是完成一项长期任务。由于我们无法跟踪进度,该栏将处于 activity 模式,我们会为每一行脉冲,但如果您可以跟踪进度,则可以改用 set_fraction 方法。
希望这会引导您朝着正确的方向前进。 GL
我正在使用这个 bash 脚本将日志文件从一个系统复制到另一个系统
#!/bin/bash s="/var/log"
d="/root/Directory" BACKUPFILE=scripts.backup.`date +%F`.tar.gz
scp -r root@:$s rsync -chavzP --stats root@ipaddr
filename=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar -czvf /$BACKUPFILE $s
tar --create --gzip --file=$d$filename $s
rm -rf /root/aaa/log
我也做过这样的进度条代码
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class ProgressBarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ProgressBar Demo")
self.set_border_width(10)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.progressbar = Gtk.ProgressBar()
vbox.pack_start(self.progressbar, True, True, 0)
button = Gtk.CheckButton("Show text")
button.connect("toggled", self.on_show_text_toggled)
vbox.pack_start(button, True, True, 0)
button = Gtk.CheckButton("Activity mode")
button.connect("toggled", self.on_activity_mode_toggled)
vbox.pack_start(button, True, True, 0)
button = Gtk.CheckButton("Right to Left")
button.connect("toggled", self.on_right_to_left_toggled)
vbox.pack_start(button, True, True, 0)
self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
self.activity_mode = False
def on_show_text_toggled(self, button):
show_text = button.get_active()
if show_text:
text = "some text"
else:
text = None
self.progressbar.set_text(text)
self.progressbar.set_show_text(show_text)
def on_activity_mode_toggled(self, button):
self.activity_mode = button.get_active()
if self.activity_mode:
self.progressbar.pulse()
else:
self.progressbar.set_fraction(0.0)
def on_right_to_left_toggled(self, button):
value = button.get_active()
self.progressbar.set_inverted(value)
def on_timeout(self, user_data):
"""
Update value on the progress bar
"""
if self.activity_mode:
self.progressbar.pulse()
else:
new_value = self.progressbar.get_fraction() + 0.01
if new_value > 1:
new_value = 0
self.progressbar.set_fraction(new_value)
# As this is a timeout function, return True so that it
# continues to get called
return True
win = ProgressBarWindow() win.connect("delete-event", Gtk.main_quit)
win.show_all() Gtk.main()
但我不知道如何将我的脚本嵌入到这个进度条代码中。
有一些解决这个问题的方法,但你必须关心的主要问题是 GUI 和耗时任务(长任务)不是好朋友。大多数 GUI 框架使用它们的主循环来处理用户输入处理并绘制 UI。话虽这么说,你必须将那些长任务与主要任务分开 UI 并且有一些方法可以做到这一点,无论是线程,异步方法等,它都会恢复到你选择的语言如何处理这些问题.
然后就是如何调用要跟踪的OS函数的问题。可能最好的方法是以编程方式在您的代码中实现它们,但这将是一项耗时的工作。因此,使用 shell 脚本将是您的选择,这样做会引出一个问题:如何获取这些命令的输出?嗯,有 popen,因为您将使用 python,那么您必须使用 popen 调用生成 shell 脚本。最好的方法似乎是 subprocess。另一个改进是更好地定制 bash 脚本,该脚本可以获得一些先前对命令结果(成功或失败)和 conditioned/formatted 输出的分析。
希望你能明白我要去哪里...你必须解析通常进入控制台的数据,解释它并相应地更新 UI。
我给你一个 Gtk Window 的简单例子,它有一个进度条,它会随着 shell 命令输出的每一行而跳动 (tree /
):
import time
import threading
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk, GObject
def app_main():
win = Gtk.Window(default_height=50, default_width=300)
win.connect("delete-event", Gtk.main_quit)
win.connect("destroy", Gtk.main_quit)
progress = Gtk.ProgressBar(show_text=True)
win.add(progress)
def update_progress(i):
progress.pulse ()
#progress.set_fraction (i/100.0) # use this for percentage
#progress.set_text(str(i))
return False
def example_target():
i = 0 # can be used to pass percentage
proc = subprocess.Popen(['tree', '/'],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line != '':
time.sleep(0.1) # output is too quick, slow down a little :)
GLib.idle_add(update_progress, i)
else:
break
win.show_all()
thread = threading.Thread(target=example_target)
thread.daemon = True
thread.start()
if __name__ == "__main__":
app_main()
Gtk.main()
在上面的例子中,我们使用了线程。您可以在控制台上尝试的 shell 命令会在您的磁盘上转储文件夹结构树。可以是任何东西,但目标是完成一项长期任务。由于我们无法跟踪进度,该栏将处于 activity 模式,我们会为每一行脉冲,但如果您可以跟踪进度,则可以改用 set_fraction 方法。
希望这会引导您朝着正确的方向前进。 GL