简体   繁体   English

使用pyQt单击按钮时,以特定顺序打开网址

[英]open urls in a certain order when clicking a button using pyQt

I'm trying to build an url opener using pyQt. 我正在尝试使用pyQt构建一个URL打开器。 Everything is fine but when i try to open multiple URLs at once they open unordered. 一切都很好,但是当我尝试一次打开多个URL时,它们无序打开。 For example I run something simillar to the code below 例如,我运行类似于下面的代码

def UK_N(self):
    if self.txt_S_UK.text() != '':
        url = 'url1'
        webbrowser.open(url)

def FR_N(self):
    if self.txt_S_FR.text() != '':
        url = 'url2'
        webbrowser.open(url)

def DE_N(self):
    if self.txt_S_DE.text() != '':
        url = 'url3'
        webbrowser.open(url)


def Open_N(self):
    if self.box_N_UK.isChecked() == True:
        self.UK_N()
    if self.box_N_FR.isChecked() == True:
        self.FR_N()
    if self.box_N_DE.isChecked() == True:
        self.DE_N()


self.btn_N_Open.clicked.connect(lambda: self.Open_N())

and what I get is 3 tabs with url3 in the first one url1 in the second and url2 in the third. 我得到的是3个选项卡,其中第一个是url3,第二个是url1,第三个是url2。 Is there a way to make these appear in the order I've programmed them? 有没有办法让它们按照我对它们的编程顺序显示?

This seems to depend on external processes that are outside of your control. 这似乎取决于您无法控制的外部流程。 Ideally, you would open the first url, wait for a notification that it had loaded, then open the next url, and so on. 理想情况下,您将打开第一个URL,等待它已加载的通知,然后打开下一个URL,依此类推。 But of course, there is no such notification, so this doesn't seem possible. 但是,当然,没有这样的通知,因此这似乎是不可能的。

Obviously, the order of the tabs depends on the nature of the resources, because they are being loaded asynchronously and some may load much faster than others. 显然,选项卡的顺序取决于资源的性质,因为它们是异步加载的,并且某些加载可能比其他加载快得多。 However, you could try to control things a little from within your program by using a timer to introduce a small delay: 但是,您可以尝试通过使用计时器引入小的延迟来在程序中稍微控制一些事情:

    QtCore.QTimer.singleShot(200, lambda: webbrowser.open(url))

I wouldn't be surprised if this didn't work, though... 但是,如果这不起作用,我不会感到惊讶。

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

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