简体   繁体   English

如何在Python中显示带有进度条的对话框

[英]How to show a dialog pane with a progress bar in Python

I'm working on Rhythmbox plugin and I need to show a dialog pane with a progress bar. 我正在使用Rhythmbox插件,并且需要显示一个带有进度条的对话框。 Here's my code : 这是我的代码:

def download_all_lyrics_action_callback(self, action):
        progressbar = Gtk.ProgressBar();

        dialog = Gtk.Dialog(_('lLyrics Preferences'), self.shell.get_property('window'),
                Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, (Gtk.STOCK_OK, Gtk.ResponseType.OK))

        content_area = dialog.get_content_area()
        content_area.pack_start(progressbar, True, True, 0)

        dialog.show_all()

        dialog.run()

        total = len(self.shell.props.library_source.props.query_model)
        i = 1;
        for row in self.shell.props.library_source.props.query_model:
            entry = row[0]
            title = entry.get_string(RB.RhythmDBPropType.TITLE)
            artist = entry.get_string(RB.RhythmDBPropType.ARTIST)
            print(title + " - " + artist)

            self.get_lyrics_for_song(title, artist)
            progressbar.set_fraction(i/total) 
        dialog.hide()

The problem is it stop in dialog.run() instruction. 问题是它停止在dialog.run()指令中。 I have also tested some code I found but without success. 我还测试了一些我发现但没有成功的代码。 Can you please help me to fix this? 你能帮我解决这个问题吗?

dialog.run() starts a main loop which runs until the user has closed the dialog or pressed one of its response buttons. dialog.run()启动一个主循环,直到用户关闭对话框或按下其响应按钮之一为止。 For what you want, it would be better to call dialog.show() instead. 对于您想要的,最好是调用dialog.show()

Make sure you run the main loop ( while Gtk.events_pending(): Gtk.main_iteration(False) ) after each call to change the progress bar; 确保在每次更改进度条的调用之后运行主循环( while Gtk.events_pending(): Gtk.main_iteration(False) ); or update the progress bar in an idle function. 或在空闲功能中更新进度条。 Otherwise you won't see the changes. 否则,您将看不到更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM