簡體   English   中英

單擊按鈕時如何使用PyQt打開URL?

[英]How can I open an URL using PyQt when clicking on a button?

我要打開http鏈接的代碼,該鏈接在URL字段中鍵入,然后鏈接到“確定”按鈕,以便我可以打開鏈接。 我對此有疑問,無法使用“確定”按鈕鏈接字段區域。 請幫忙。

def __init__(self):
    super(Example, self).__init__()

    self.initUI()
def initUI(self):

    b1 = QtGui.QPushButton("OK", self)
    b1.clicked.connect(open_webbrowser)
    b1.move(100,100)
    b2 =QtGui.QPushButton("EXIT", self)
    b2.clicked.connect(self.buttonClicked2)
    b2.move(300,100)


    l1 = QtGui.QLabel('URL',self)
    l1.move(100,20)
    self.add1 = QtGui.QLineEdit()


    l2 = QtGui.QLabel("PORT",self)
    l2.move(100,50)
    self.add2 = QtGui.QLineEdit()

    fbox = QtGui.QFormLayout()
    vbox = QtGui.QVBoxLayout() 

    vbox.addWidget(self.add1)
    vbox.addWidget(self.add2)
    fbox.addRow(l1,self.add1)
    fbox.addRow(l2,self.add2)   

    self.setLayout(fbox)
    self.setWindowTitle('URL STREAM')
    self.show()


def buttonClicked1(self):
    print self.add1.text()
    print self.add2.text()
    print "Streaming..."

def open_webbrowser(self):
    webbroser.open('http://www.google.com') 



def buttonClicked2(self):
    print "Exiting..."
    self.close()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.initUI()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()

您需要從行編輯獲取文本,例如:

def open_webbrowser(self):
    # if you want please remember to check its valid url or not
    url = str(self.add1.text())
    webbroser.open(url) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM