如何使用 PyQt 制作可调整大小的 window 和可调整大小的背景图像?
How do I make a resizable window and a resizable background image using PyQt?
我试图制作一个应用程序,所以我开始构建主要的 window,我花了一些时间来弄清楚如何放置背景图片,但最后我明白了,但是当我点击最大化 window 我注意到按钮没有像以前那样居中,背景图像也没有根据 window 的不同大小调整大小。我希望背景图像和按钮能够自动调整。
请帮助这个可怜的新手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()
您需要一些布局来放置您的小部件,并且您需要设置对齐方式 also.I 编辑了您的代码位以获得您要查找的内容。我还编辑了关闭事件。
从 PyQt4 导入 QtCore、QtGui
导入系统
# 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()
我试图制作一个应用程序,所以我开始构建主要的 window,我花了一些时间来弄清楚如何放置背景图片,但最后我明白了,但是当我点击最大化 window 我注意到按钮没有像以前那样居中,背景图像也没有根据 window 的不同大小调整大小。我希望背景图像和按钮能够自动调整。 请帮助这个可怜的新手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()
您需要一些布局来放置您的小部件,并且您需要设置对齐方式 also.I 编辑了您的代码位以获得您要查找的内容。我还编辑了关闭事件。
从 PyQt4 导入 QtCore、QtGui 导入系统
# 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()