我正在尝试制作一个应用程序所以我开始构建主窗口,我花了一些时间来弄清楚如何放置背景图像但最终我得到了它,但是当我点击以最大化窗口时我注意到了按钮不像以前那样位于中心,并且背景图像也不会根据窗口的不同大小调整大小。我希望背景图像和按钮自动调整。 请帮助这个可怜的新手T.T
这是我的代码:
from PyQt4 import QtCore, QtGui
import sys
# Encoding Utf-8*
try:
_from_utf8 = QtCore.QString.fromUtf8
except AttributeError:
def _from_utf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
# Main Window
class Window (QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle(_from_utf8("Virtual Library"))
self.setStyleSheet("background-image: url(nwlib.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))
# AccessLibrary btn
access_lib_btn = QtGui.QPushButton("Access Library", self)
access_lib_btn.setGeometry(QtCore.QRect(310, 250, 141, 41))
# FindNewBooks btn
find_nbooks = QtGui.QPushButton("Find New Books", self)
find_nbooks.setGeometry(QtCore.QRect(310, 320, 141, 41))
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()
答案 0 :(得分:1)
你需要一些布局来放置你的小部件,你也需要设置对齐。我编辑了你的代码位以获得你想要的东西。我也编辑了关闭事件。
从PyQt4导入QtCore,QtGui import sys
# Encoding Utf-8*
try:
_from_utf8 = QtCore.QString.fromUtf8
except AttributeError:
def _from_utf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
# Main Window
class Window (QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle(_from_utf8("Virtual Library"))
self.setStyleSheet("background-image: url(nwlib.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
centralwidget = QtGui.QWidget(self)
self.mainLayout = QtGui.QVBoxLayout(centralwidget)
new_lib_btn = QtGui.QPushButton("New Library", self)
new_lib_btn.setMinimumSize(141, 41)
# AccessLibrary btn
access_lib_btn = QtGui.QPushButton("Access Library", self)
access_lib_btn.setMinimumSize(141, 41)
# FindNewBooks btn
find_nbooks = QtGui.QPushButton("Find New Books", self)
find_nbooks.setMinimumSize(141, 41)
self.mainLayout.addWidget(new_lib_btn)
self.mainLayout.addWidget(access_lib_btn)
self.mainLayout.addWidget(find_nbooks)
self.mainLayout.setAlignment(QtCore.Qt.AlignCenter)
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 run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()