python实现点击按钮切换到另外一个界面_如何使用PyQt从另一个窗口单击按钮打开窗口?...

我正在试着做一个应用程序,但是我一直被“简单”的事情所困扰,比如这个,我如何通过点击一个按钮来打开一个新窗口?我试过使用new_lib_btn.clicked.connect(newlib),newlib是包含我的第二个窗口的文件,new_lib_btn是应该打开窗口的按钮,它在我的主窗口中,您可以在下面看到:

mainwindow.pyfrom PyQt4 import QtCore, QtGui

import newlib

import sys

# Main Window

class Window (QtGui.QMainWindow):

def __init__(self):

super(Window, self).__init__()

centralwidget = QtGui.QWidget(self)

self.mainLayout = QtGui.QVBoxLayout(centralwidget)

self.mainLayout.setAlignment(QtCore.Qt.AlignCenter)

self.setCentralWidget(centralwidget)

self.resize(800, 600)

self.setWindowTitle("Virtual Library")

self.setStyleSheet("Window {border-image: url(lib.jpg);}")

# ExitOption

menu_action1 = QtGui.QAction("Exit", self)

menu_action1.setShortcut("Ctrl+Q")

menu_action1.setStatusTip('Exit The App')

menu_action1.triggered.connect(self.close_application)

self.statusBar()

# MenuBar

main_menu = self.menuBar()

file_menu = main_menu.addMenu('Options')

file_menu.addAction(menu_action1)

self.home()

def home(self):

# NewLibrary btn

new_lib_btn = QtGui.QPushButton("New Library", self)

new_lib_btn.setGeometry(QtCore.QRect(310, 180, 141, 41))

new_lib_btn.setStyleSheet("color: black;")

# AccessLibrary btn

access_lib_btn = QtGui.QPushButton("Access Library", self)

access_lib_btn.setGeometry(QtCore.QRect(310, 250, 141, 41))

access_lib_btn.setStyleSheet("color: black;")

# FindNewBooks btn

find_nbooks = QtGui.QPushButton("Find New Books*", self)

find_nbooks.setGeometry(QtCore.QRect(310, 320, 141, 41))

find_nbooks.setStyleSheet("color: black;")

self.mainLayout.addWidget(new_lib_btn)

self.mainLayout.addWidget(access_lib_btn)

self.mainLayout.addWidget(find_nbooks_btn)

self.show()

def close_application(self):

choice = QtGui.QMessageBox.question(self, 'Exit',

"Close the application?",

QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

if choice == QtGui.QMessageBox.Yes:

sys.exit()

else:

pass

def run():

app = QtGui.QApplication(sys.argv)

GUI = Window()

sys.exit(app.exec_())

run()

这是我的第二个窗口,我想用new_lib_btn打开的那个

newlib.pyclass NewLibrary (QtGui.QMainWindow):

def __init__(self):

super(NewLibrary, self).__init__()

self.resize(800,600)

self.setWindowTitle("New Library")

self.setStyleSheet("NewLibrary {border-image: url(wood.jpg);}")

# File Options

file_action1 = QtGui.QAction("New Library", self)

file_action1.setShortcut("Ctrl+N")

file_action1.setStatusTip("Creates a new library")

file_action2 = QtGui.QAction("Exit this!", self)

file_action2.setShortcut("Ctrl+Q")

file_action2.setStatusTip("Closes The App")

file_action2.triggered.connect(self.close_application)

#File Menu

main_menu = self.menuBar()

file_menu = main_menu.addMenu("File")

file_menu.addAction(file_action1)

file_menu.addAction(file_action2)

self.newLib()

self.newLib()

def newLib(self):

centralwidget = QtGui.QWidget(self)

self.mainLayout = QtGui.QVBoxLayout(centralwidget)

self.mainLayout.setAlignment(QtCore.Qt.AlignCenter)

#some useful buttons in the future

self.setCentralWidget(centralwidget)

self.show()

def close_application(self):

choice = QtGui.QMessageBox.question(self, 'Exit',

"Close the application?",

QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

if choice == QtGui.QMessageBox.Yes:

sys.exit()

else:

pass

def runNewLib():

app = QtGui.QApplication(sys.argv)

gui = NewLibrary()

sys.exit(app.exec_())

runNewLib()

我搜索了很多关于这个的信息,但是我不能理解其中一些与我的情况有点接近的信息,所以我请求帮助,这看起来很简单,但是我没有得到:/,我应该怎么做才能通过单击new_lib_btn打开第二个窗口?请帮忙。

你可能感兴趣的:(python实现点击按钮切换到另外一个界面_如何使用PyQt从另一个窗口单击按钮打开窗口?...)