问题描述
我正在尝试制作一个简单的转换器.我使用了 PyQt4 设计来制作 Gui我想知道点击单个按钮后如何启动一个新窗口.
HI I am trying to make a simple converter.I have used PyQt4 designed to make the GuiI want to know how launch a new window after I click on the individual button.
这是我使用 PyQt4 Designer 创建的界面.这是图片链接:
This is the interface I have created using PyQt4 Designer.Here is the Image link :
我想在我点击货币按钮时启动这个窗口.这是图片链接:
and I want to launch this windows when I click on currency button.Here is the Image Link:
这是我的 main.py
from PyQt4 import QtGui
from main_screen import mainscreen
def main():
import sys
qApp = QtGui.QApplication(sys.argv)
aw = mainscreen()
aw.show()
sys.exit(qApp.exec_())
if __name__ == '__main__':
main()
和 mainscreen.py
from PyQt4 import QtCore, QtGui
from window_main import Ui_MainWindow
class mainscreen(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(mainscreen,self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
点击货币按钮后如何打开新窗口(货币按钮的对象名称为currency_bt")我必须在同一窗口中编写货币代码还是必须在新窗口中编写.我该怎么做.
How can I open new window after I click on currency button (object name for currency button is "currency_bt")and do I have to write the code for currency in same window or I have to write in new window.How do I do it.
我是 Python Gui 编程的新手.
I am new to Python Gui programming.
推荐答案
您在 Qt Designer 中创建的每个 GUI 表单都需要使用 pyuic 转换为 Python 模块.因此,首先,您需要对 currency.ui
执行与对 window_main
执行的操作相同的操作.
Each GUI form that you create in Qt Designer needs to be converted into a python module using pyuic. So, to start with, you need to do the same for currency.ui
that you did for window_main
.
现在您可以将货币窗口类导入mainscreen.py
,并将按钮连接到处理程序以便您可以显示它.
Now you can import your currency window class into mainscreen.py
, and connect a button to handler so you can display it.
代码看起来像这样:
from PyQt4 import QtCore, QtGui
from window_main import Ui_MainWindow
from currency import Ui_CurrencyWindow
class CurrencyWindow(QtGui.QMainWindow, Ui_CurrencyWindow):
def __init__(self, parent=None):
super(CurrencyWindow, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setupUi(self)
class MainScreen(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainScreen, self).__init__(parent)
self.setupUi(self)
self.currencyButton.clicked.connect(self.handleCurrencyButton)
def handleCurrencyButton(self):
window = CurrencyWindow(self)
window.show()
看过这个示例代码后,你可能会想到你最终会导入很多的模块,并且有很多的锅炉——为每个人编写板代码(这不是很有趣).
After looking at this example code, it will probably occur to you that you are going to end up importing a lot of modules, and have a lot of boiler-plate code to write for each one of them (which is not much fun).
因此,我建议您考虑更改 GUI 设计,以便您拥有一个包含 tabwidget 的主窗口,然后为每个转换器创建一个单独的选项卡.这不仅会使您的应用程序更容易编写,而且还应该使它更好用.
So I would advise you to consider changing your GUI design, so that you have one main window containing a tabwidget, and then have a separate tab for each of your converters. This will not only make your application much easier to write, but it should also make it a lot nicer to use.
这篇关于在 PyQt4 中单击主屏幕上的按钮后如何打开子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!