linux – 无法初始化窗口并等待进程以Python 3 GTK 3结束

linux – 无法初始化窗口并等待进程以Python 3 GTK 3结束,第1张

概述我是面向对象编程, Python和GTK 3的新手,尽管我对程序编程(主要是C)有很好的了解. 我正在尝试构建一个简单的Python GTK 3脚本来在Linux下运行pkexec apt-get update. 我有一个mainWindow类(基于Gtk.Window类),它包含一个名为button的按钮对象(基于Gtk.Button类),它触发了一个在单击事件时在mainWindow中定义的n 我是面向对象编程,Python和GTK 3的新手,尽管我对程序编程(主要是C)有很好的了解.

我正在尝试构建一个简单的Python GTK 3脚本来在linux下运行pkexec apt-get update.

我有一个mainWindow类(基于Gtk.Window类),它包含一个名为button的按钮对象(基于Gtk.button类),它触发了一个在单击事件时在mainWindow中定义的new_update_window()方法;

new_update_window()方法从updateWindow类(基于Gtk.Window类)初始化updateWindow对象,该类包含名为label的标签对象(基于Gtk.Label类)并调用方法show_all()和update()定义在updateWindow中;

update()方法应该更改标签,运行pkexec apt-get update并再次更改标签.

问题是无论我做什么,都会发生以下情况之一:

>如果我直接运行subprocess.Popen([“/usr/bin/pkexec”,“/usr/bin/apt-get”,“update”]),则会显示update.Window但是标签会立即设置为值只有在pkexec apt-get update完成执行后才能设置;
>如果我直接运行subprocess.call([“/usr/bin/pkexec”,直到pkexec apt-get update完成后才会显示update.Window执行;
>我尝试导入线程,在updateWindow中定义一个单独的run_update()方法,并使用thread = threading.Thread(target = self.run_update),thread.start(),thread.join()在单独的线程中启动该函数,但是仍然取决于我在run_update()(subprocess.call()或subprocess.Popen)中调用的方法,上面描述的相对行为表现出来.

文艺青年最爱的

我无法理解如何完成我所追求的目标,即:

>显示updateWindow(Gtk.Window)
>在updateWindow中更新标签(Gtk.Label)
>运行pkexec apt-get update
>在updateWindow中更新标签

> subprocess.Popen():显示update.Window但是标签立即设置为只有在pkexec apt-get update完成执行后才应设置的值;
> subprocess.call():在pkexec apt-get update完成执行之前,不会显示update.Window;
>在函数中包装两个中的任何一个并在单独的线程中运行该函数不会改变任何东西.

这是代码;

不使用线程(案例1,在本例中使用subprocess.Popen()):

#!/usr/bin/python3from gi.repository import Gtkimport subprocessclass mainWindow(Gtk.Window):    def __init__(self):        Gtk.Window.__init__(self,Title = "Updater")        button = Gtk.button()        button.set_label("Update")        button.connect("clicked",self.new_update_window)        self.add(button)    def new_update_window(self,button):        update = updateWindow()        update.show_all()        update.update()class updateWindow(Gtk.Window):    def __init__(self):        Gtk.Window.__init__(self,Title = "Updating...")        self.label = Gtk.Label()        self.label.set_text("IDling...")        self.add(self.label)    def update(self):        self.label.set_text("Updating... Please wait.")        subprocess.call(["/usr/bin/pkexec","/usr/bin/apt-get","update"])        self.label.set_text("Updated.")    def run_update(self):main = mainWindow()main.connect("delete-event",Gtk.main_quit)main.show_all()Gtk.main()

使用线程(案例3,在本例中使用subprocess.Popen()):

#!/usr/bin/python3from gi.repository import Gtkimport threadingimport subprocessclass mainWindow(Gtk.Window):    def __init__(self):        Gtk.Window.__init__(self,Title = "Updating...")        self.label = Gtk.Label()        self.label.set_text("IDling...")        self.add(self.label)    def update(self):        self.label.set_text("Updating... Please wait.")        thread = threading.Thread(target=self.run_update)        thread.start()        thread.join()        self.label.set_text("Updated.")    def run_update(self):        subprocess.Popen(["/usr/bin/pkexec","update"])main = mainWindow()main.connect("delete-event",Gtk.main_quit)main.show_all()Gtk.main()
解决方法 您可以使用 Gio.Subprocess与GTK的主循环集成,而不是使用Python的子进程模块:

#!/usr/bin/python3from gi.repository import Gtk,Gio# ...class updateWindow(Gtk.Window):    def __init__(self):        Gtk.Window.__init__(self,title="Updating...")        self.label = Gtk.Label()        self.label.set_text("IDling...")        self.add(self.label)    def update(self):        self.label.set_text("Updating... Please wait.")        subprocess = Gio.Subprocess.new(["/usr/bin/pkexec","update"],0)        subprocess.wait_check_async(None,self._on_update_finished)    def _on_update_finished(self,subprocess,result):        subprocess.wait_check_finish(result)        self.label.set_text("Updated.")
总结

以上是内存溢出为你收集整理的linux – 无法初始化窗口并等待进程以Python 3 GTK 3结束全部内容,希望文章能够帮你解决linux – 无法初始化窗口并等待进程以Python 3 GTK 3结束所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1023918.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存