[英]Open Window when Clicked on Push Button in Main Window PyQt5 Python
我已經瀏覽了有類似問題的各種類似帖子,也嘗試了答案,但它對我沒有用。 所以這是我的問題 -
I have two windows - TestBox and MailBox, when i click on the TestBox Pushbutton along with Input Path argument, it has to open the MailBox Window, This new window MailBox will send the ModifiedPath to the TestBox again when i closes the MailBox window
I have created both windows in PyQt5 Tool and created ui file and then converted to the Python file using - python -m PyQt5.uic.pyuic testboxui.ui -o testboxui.py
我沒有觸及 testboxui.py 或 mailboxui.py 文件,因為它們不斷更改通過重新轉換完成的任何修改。 相反,我創建了另一個文件 TestBox.py 和 MailBox.py 來導入和編寫函數。
這是可生產的最小代碼 -
TestBox.py [主要應用代碼]
import os,sys
from PyQt5 import QtWidgets, QtGui, QtCore
from testboxui import Ui_TestBox
from MailBox import AppWindow_MailList
app = QtWidgets.QApplication(sys.argv)
class AppWindow_MainBox(QtWidgets.QMainWindow):
def __init__(self):
super(AppWindow_MainBox, self).__init__()
self.ui = Ui_TestBox()
self.ui.setupUi(self)
self.ui.openbox.clicked.connect(self.fcn_maillist)
def fcn_maillist(self):
GetPath = os.getcwd()
dialog = AppWindow_MailList(self,GetPath)
if dialog.exec_() == AppWindow_MailList.Accepted:
GetPath = dialog.get_output()
def main():
app = QtWidgets.QApplication(sys.argv)
application = AppWindow_MainBox()
application.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
testboxui.py [由 PyQt5 工具直接從 UI 文件生成]
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_TestBox(object):
def setupUi(self, TestBox):
TestBox.setObjectName("TestBox")
TestBox.resize(647, 279)
self.centralwidget = QtWidgets.QWidget(TestBox)
self.centralwidget.setObjectName("centralwidget")
self.openbox = QtWidgets.QPushButton(self.centralwidget)
self.openbox.setGeometry(QtCore.QRect(210, 60, 231, 91))
self.openbox.setObjectName("openbox")
TestBox.setCentralWidget(self.centralwidget)
self.retranslateUi(TestBox)
QtCore.QMetaObject.connectSlotsByName(TestBox)
def retranslateUi(self, TestBox):
_translate = QtCore.QCoreApplication.translate
TestBox.setWindowTitle(_translate("TestBox", "TestBox"))
self.openbox.setText(_translate("TestBox", "Click Here \n""To Open Another Window"))
MailBox.py [子申請代碼]
import os,sys
from PyQt5 import QtWidgets, QtGui, QtCore
from maillistui import Ui_MailList
app = QtWidgets.QApplication(sys.argv)
class AppWindow_MailList(QtWidgets.QMainWindow):
def __init__(self,RcvPath=''):
super(AppWindow_MailList, self).__init__()
self.ui = Ui_MailList()
self.ui.setupUi(self)
self.init_fcn(RcvPath)
def init_fcn(self,RcvPath):
self.ui.browse.clicked.connect(self.browse_fcn)
if not RcvPath:
self.RcvPath = os.getcwd()
else:
self.RcvPath = RcvPath
self.ui.path.setText(self.RcvPath)
def closeEvent(self, event):
event.accept()
def get_output(self):
return os.path.join(self.RcvPath,'TestFolder')
def browse_fcn(self):
pass
def main():
app = QtWidgets.QApplication(sys.argv)
application = AppWindow_MailList()
application.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
maillistui.py - [由 PyQt5 直接從 ui 文件生成]
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MailList(object):
def setupUi(self, MailList):
MailList.setObjectName("MailList")
MailList.resize(647, 279)
self.centralwidget = QtWidgets.QWidget(MailList)
self.centralwidget.setObjectName("centralwidget")
self.path = QtWidgets.QLineEdit(self.centralwidget)
self.path.setGeometry(QtCore.QRect(50, 30, 511, 41))
self.path.setObjectName("path")
self.browse = QtWidgets.QPushButton(self.centralwidget)
self.browse.setGeometry(QtCore.QRect(450, 90, 112, 41))
self.browse.setObjectName("browse")
MailList.setCentralWidget(self.centralwidget)
self.retranslateUi(MailList)
QtCore.QMetaObject.connectSlotsByName(MailList)
def retranslateUi(self, MailList):
_translate = QtCore.QCoreApplication.translate
MailList.setWindowTitle(_translate("MailList", "MailList"))
self.browse.setText(_translate("MailList", "Browse"))
以前我曾經用 QtWidgets.QDialog 制作 PyQt5 對話框,我可以在 QtWidgets.QApplication 中輕松加載。 但是這里兩者都是 QApplication (QMainWindow) 但很難在 UI 中調用 ui。
我在這里做錯什么了嗎???
只應創建一個 QApplication ,在您的情況下,對於您不必要地創建 2 個腳本,請消除以下代碼:
```
from MailBox import AppWindow_MailList
app = QtWidgets.QApplication(sys.argv) # <--- delete this line
class AppWindow_MainBox(QtWidgets.QMainWindow):
```
```
from maillistui import Ui_MailList
app = QtWidgets.QApplication(sys.argv) # <--- delete this line
class AppWindow_MailList(QtWidgets.QMainWindow):
```
更正上述情況,出現另一個錯誤,但是對於這個 Python 指出錯誤消息,在許多 IDE 的情況下它們不處理 Qt 錯誤,所以我建議您在控制台中運行它,您將收到以下錯誤消息:
Traceback (most recent call last):
File "TextBox.py", line 16, in fcn_maillist
dialog = AppWindow_MailList(self,GetPath)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
這清楚地表明 AppWindow_MailList class 接受一個參數( RcvPath
),但你傳遞了它 2。我不知道這是一個錯字還是你不知道在 python 中使用“self”(如果是后者,那么推薦閱讀“self”這個詞的目的是什么? )
class AppWindow_MailList(QtWidgets.QMainWindow):
def __init__(self,RcvPath=''):
super(AppWindow_MailList, self).__init__()
即使解決了這個錯誤,還有另一個問題,AppWindow_MailList 是一個 QMainWindow,所以它沒有任何 exec_() 方法,似乎你試圖使用一些使用 QDialog 的帖子的代碼而不了解解決方案的邏輯。
一般來說,每個小部件都有一個用途:
QWidget 是一個通用小部件,可用作構建任何其他類型的小部件或容器的基礎,類似於 html 中的 div。
QDialog 是一個小部件,其目的是向用戶請求信息,因此 exec_() 方法返回阻塞事件循環的請求狀態(如果它被接受或拒絕)。
QMainWindow 的目的是提供一個主要的 window,因為它包含工具欄、狀態欄、菜單欄、dockwidets 等。
因此,要實現您的目標,您必須選擇正確的元素,考慮到我將重組您的應用程序。
郵箱.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>461</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QLineEdit" name="mail_le">
<property name="minimumSize">
<size>
<width>0</width>
<height>49</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>40</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>324</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="browse_btn">
<property name="minimumSize">
<size>
<width>110</width>
<height>40</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>110</height>
</size>
</property>
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>178</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
測試箱.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>213</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>267</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="open_btn">
<property name="minimumSize">
<size>
<width>230</width>
<height>90</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>230</width>
<height>90</height>
</size>
</property>
<property name="text">
<string>Click Here
To Open Another Window</string>
</property>
</widget>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>267</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>212</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
python -m PyQt5.uic.pyuic mailbox.ui -o mailbox_ui.py -x
python -m PyQt5.uic.pyuic testbox.ui -o testbox_ui.py -x
郵箱.py
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from mailbox_ui import Ui_Dialog
class MailBox(QtWidgets.QDialog):
def __init__(self, mail="", parent=None):
super().__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.browse_btn.clicked.connect(self.accept)
if mail:
self.ui.mail_le.setText(mail)
else:
self.ui.mail_le.setText(os.getcwd())
def mail(self):
return self.ui.mail_le.text()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
w = MailBox()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
測試箱.py
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from mailbox import MailBox
from testbox_ui import Ui_MainWindow
class TestBox(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.open_btn.clicked.connect(self.open_window)
@QtCore.pyqtSlot()
def open_window(self):
dialog = MailBox()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
path = dialog.mail()
print(path)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
w = TestBox()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
├── mailbox.py
├── mailbox.ui
├── mailbox_ui.py
├── testbox.py
├── testbox.ui
└── testbox_ui.py
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.