关闭并从自定义对话框PyQT5获取数据

时间:2015-11-26 19:37:32

标签: python qt pyqt

我无法使用Qt Designer制作自定义对话框。我知道如何弹出对话框,但我无法从该行文本中获取数据。

  

主程序.py

from PyQt5 import QtCore, QtGui, QtWidgets
from ui import Ui_MainWindow
from addui import Ui_Dialog as Form
from bs4 import BeautifulSoup
import requests
import time
import sys


class MainDialog(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
    super(self.__class__, self).__init__()
    self.setupUi(self)
    self.actionRefresh.triggered.connect(self.refresh_btn)
    self.actionAdd.triggered.connect(self.add_btn)
    self.actionRemove.triggered.connect(self.remove_btn)
    self.actionSettings.triggered.connect(self.settings_btn)
    self.actionAbout.triggered.connect(self.about_btn)
    self.actionExit.triggered.connect(self.exit_btn)

def open_dialog(self):
    dialog = QtWidgets.QDialog()
    dialog.ui = Form()
    dialog.ui.setupUi(dialog)
    dialog.exec_()
    dialog.show()

def refresh_btn(self):
    print('Refresh')
    self.getting_data()

def add_btn(self):
    print('Add')
    self.open_dialog()

def remove_btn(self):
    print('Remove')

def settings_btn(self):
    print('Settings')
    QtWidgets.QMessageBox.warning(self, 'Settings',
                                  'Work in progress.\n'
                                  '   Coming soon!')

def about_btn(self):
    print('About')
    QtWidgets.QMessageBox.about(self, 'About Checking Prices',
                                'Checking Prices - Beta v1.0\n'
                                '\n'
                                'Copyright(c) 2015 - Pifu Valentin')

def exit_btn(self):
    self.close()

def getting_data(self):
    links = ['link1',
             'link2',
             'link3'
             ]
    self.statusBar.showMessage('Getting data...')
    try:
        for nr, link in enumerate(links, start=1):
            cont = requests.get(link)
            soup = BeautifulSoup(cont.content, "html.parser")
            title = soup.title.text[:40]
            price = soup.find('span', {'itemprop': 'price'}).text
            linetxt = ('{}. {} >> ${}').format(nr, title, price)
            if nr == 1:
                self.linetxt1.setText(linetxt)
            elif nr == 2:
                self.linetxt2.setText(linetxt)
            elif nr == 3:
                self.linetxt3.setText(linetxt)
        self.statusBar.showMessage('Last updated - '+time.strftime('%H:%M:%S'))
    except:
        self.statusBar.showMessage('Error getting data.')


def main():
     app = QtWidgets.QApplication(sys.argv)
     form = MainDialog()
     form.show()
     app.exec_()

if __name__ == '__main__':
     main()
  

addui.py(对话框)

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(200, 71)
    Dialog.setMinimumSize(QtCore.QSize(200, 71))
    Dialog.setMaximumSize(QtCore.QSize(200, 71))
    Dialog.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap("Icons/Plus-32.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    Dialog.setWindowIcon(icon)
    self.gridLayout = QtWidgets.QGridLayout(Dialog)
    self.gridLayout.setObjectName("gridLayout")
    self.text_link = QtWidgets.QLineEdit(Dialog)
    self.text_link.setObjectName("text_link")
    self.gridLayout.addWidget(self.text_link, 0, 0, 1, 2)
    self.add_link = QtWidgets.QPushButton(Dialog)
    self.add_link.setObjectName("add_link")
    self.gridLayout.addWidget(self.add_link, 1, 0, 1, 1)
    self.cancel_link = QtWidgets.QPushButton(Dialog)
    self.cancel_link.setObjectName("cancel_link")
    self.gridLayout.addWidget(self.cancel_link, 1, 1, 1, 1)
    self.retranslateUi(Dialog)
    self.cancel_link.clicked.connect(self.exit_dialog)
    self.add_link.clicked.connect(self.get_link)

def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Add link"))
    self.add_link.setText(_translate("Dialog", "Add"))
    self.cancel_link.setText(_translate("Dialog", "Cancel"))

def get_link(self):
    print(self.text_link.text())
    x = self.text_link.text()
    return x

def exit_dialog(self):
    self.destroy()

我对这个程序有些问题。

  1. 如果我点击取消只退出对话框而不是主程序。 (我尝试过self.close,self.hide ......)

  2. 我想在line_text上添加一个链接,并获得指向主程序的链接,但是当我点击添加到关闭对话框并将数据传递给主程序时。

  3. 我怎么称呼对话?

     def open_dialog(self):
       dialog = QtWidgets.QDialog()
       dialog.ui = Form()
       dialog.ui.setupUi(dialog)
       dialog.exec_()
       dialog.show()
    
  4. 感谢。我不知道如何做到这一点,希望有人可以提供帮助。 再次感谢。

1 个答案:

答案 0 :(得分:0)

你需要exec_()来做这个,这是你修改的代码的最小版本     来自PyQt4导入QtCore,QtGui     import sys

class PopUpDLG(QtGui.QDialog):
    def __init__(self):
        super(PopUpDLG, self).__init__()
        self.setObjectName("self")
        self.resize(200, 71)
        self.setMinimumSize(QtCore.QSize(200, 71))
        self.setMaximumSize(QtCore.QSize(200, 71))
        self.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Icons/Plus-32.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.gridLayout = QtGui.QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")
        self.text_link = QtGui.QLineEdit(self)
        self.text_link.setObjectName("text_link")
        self.gridLayout.addWidget(self.text_link, 0, 0, 1, 2)
        self.add_link = QtGui.QPushButton(self)
        self.add_link.setObjectName("add_link")
        self.gridLayout.addWidget(self.add_link, 1, 0, 1, 1)
        self.cancel_link = QtGui.QPushButton(self)
        self.cancel_link.setObjectName("cancel_link")
        self.gridLayout.addWidget(self.cancel_link, 1, 1, 1, 1)
        self.retranslateUi(self)
        self.cancel_link.clicked.connect(self.reject)
        self.add_link.clicked.connect(self.get_link)
        self.retrunVal = None

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Dialog", "Add link"))
        self.add_link.setText(_translate("Dialog", "Add"))
        self.cancel_link.setText(_translate("Dialog", "Cancel"))

    def get_link(self):
        self.retrunVal = self.text_link.text()
        self.accept()

    def exec_(self):
        super(PopUpDLG, self).exec_()
        return self.retrunVal


class MainDialog(QtGui.QMainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        centralwidget = QtGui.QWidget(self)
        self.layout = QtGui.QHBoxLayout(centralwidget)
        self.button = QtGui.QPushButton("Open")
        self.valText = QtGui.QLabel("")
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.valText)
        self.setCentralWidget(centralwidget)
        self.button.clicked.connect(self.open_dialog)

    def open_dialog(self):
        dialog = PopUpDLG()
        value = dialog.exec_()
        if value:
            self.valText.setText(value)


def main():
     app = QtGui.QApplication(sys.argv)
     form = MainDialog()
     form.show()
     app.exec_()

if __name__ == '__main__':
     main()