简体   繁体   中英

open urls in a certain order when clicking a button using pyQt

I'm trying to build an url opener using pyQt. Everything is fine but when i try to open multiple URLs at once they open unordered. 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. 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. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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