0
我正在嘗試做一個應用程序,所以我開始構建主窗口,我花了一些時間來弄清楚如何放置背景圖片,但最終我得到了它,但是當我點擊以最大化窗口時,我注意到按鈕不在以前的中心位置,並且背景圖像也不會根據窗口的大小調整大小。我想要背景圖像和按鈕自動調整。 請幫助這個可憐的新手T.T如何使用PyQt製作可調整大小的窗口和可調整大小的背景圖像?
這裏是我的代碼:你需要一些佈局,以將您的小部件
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()
嘿,非常感謝! :) –