我的代码
import os
import sys
import random
import string
from PyQt5.QtWidgets import QLabel, QStatusBar, QMenuBar, QAction, QTabWidget, QFormLayout, QWidget, QDialog, QVBoxLayout, QMainWindow, QGridLayout, QPushButton, QListView, QHBoxLayout
from PyQt5.QtCore import QDateTime, Qt
from PyQt5 import QtWidgets, QtGui, QtCore
class Book:
def __init__(self, title, author, image):
self.title = title
self.author = author
self.image = image
self.code = self.generateCode()
def generateCode(self):
code = "".join(random.choices(string.ascii_letters + string.digits, k=8))
return code
class BookList(QListView):
def __init__(self, books):
super().__init__()
self.books = books
self.model = QtGui.QStandardItemModel()
self.populate_model()
self.setModel(self.model)
self.setSpacing(10)
self.setViewMode(QListView.IconMode)
self.setResizeMode(QListView.Adjust)
self.setIconSize(QtCore.QSize(220, 250))
self.setWordWrap(True)
self.clicked.connect(self.book_clicked)
def populate_model(self):
for book in self.books:
item = QtGui.QStandardItem()
item.setText(book.title + '\n' + book.author)
pixmap = QtGui.QPixmap(book.image)
item.setIcon(QtGui.QIcon(pixmap))
item.setData(book)
self.model.appendRow(item)
def book_clicked(self, index):
book = index.data()
self.borrow_book(book)
def borrow_book(self, book):
name, ok = QtWidgets.QInputDialog.getText(
self, "Borrow Book", "Enter your name:"
)
if ok:
library = Library()
library.show_receipt(book, name)
class Library(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Interactive Library")
self.receiptDialog = QDialog(self)
self.receiptDialog.setMinimumSize(2300, 2300)
self.setGeometry(100, 100, 1500, 1200)
self.layout = QVBoxLayout(self.receiptDialog)
self.tab_widget = QTabWidget(self)
# set the layout of the Library widget
layout = QVBoxLayout(self)
layout.addWidget(self.tab_widget)
self.show()
# Create tab widget
self.tab_widget = QtWidgets.QTabWidget()
self.tab_widget.setFixedSize(1300, 900)
# Create home tab
self.home_tab = QtWidgets.QWidget()
self.tab_widget.addTab(self.home_tab, "")
# Set icon for home tab
icon_home = QtGui.QIcon("icons/home.png")
self.tab_widget.setTabIcon(0, icon_home)
# Create book tab
self.book_tab = BookList(
[
Book(
"David Goggins Can't Hurt Me",
"Author: David Goggins",
"image/David Goggins Can't Hurt Me.jpg",
),
Book(
"The Power of Now",
"Author: Eckhart Tolle",
"image/The Power of Now.jpg",
),
]
)
self.tab_widget.addTab(self.book_tab, "")
# Set icon for book tab
icon_book = QtGui.QIcon("icons/books.png")
self.tab_widget.setTabIcon(1, icon_book)
# Create settings tab
self.settings_tab = QtWidgets.QWidget()
self.tab_widget.addTab(self.settings_tab, "")
# Set icon for settings tab
icon_settings = QtGui.QIcon("icons/settings.png")
self.tab_widget.setTabIcon(2, icon_settings)
# Create support tab
self.support_tab = QtWidgets.QWidget()
self.tab_widget.addTab(self.support_tab, "")
# Set icon for support tab
icon_support = QtGui.QIcon("icons/support.png")
self.tab_widget.setTabIcon(3, icon_support)
# Create profile tab
self.profile_tab = QtWidgets.QWidget()
self.tab_widget.addTab(self.profile_tab, "")
# Set icon for profile tab
icon_profile = QtGui.QIcon("icons/profile.png")
self.tab_widget.setTabIcon(4, icon_profile)
self.tab_widget.setStyleSheet("QTabBar::tab { max-width: 1500px; max-height: 1500px; }")
# Set tab widget as central widget
self.setCentralWidget(self.tab_widget)
# Set up status bar
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
# Set up menu bar
self.menu_bar = QMenuBar()
self.setMenuBar(self.menu_bar)
# Set up file menu
self.file_menu = self.menu_bar.addMenu("File")
# Create exit action
self.exit_action = QAction("Exit", self)
self.exit_action.setShortcut("Ctrl+Q")
self.exit_action.triggered.connect(self.close)
# Add exit action to file menu
self.file_menu.addAction(self.exit_action)
# Set up edit menu
self.edit_menu = self.menu_bar.addMenu("Edit")
# Set up help menu
self.help_menu = self.menu_bar.addMenu("Help")
def show_receipt(self, book, name):
currentDateTime = QDateTime.currentDateTime()
currentDateTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss")
deadline = currentDateTime.addDays(14)
deadlineString = deadline.toString("yyyy-MM-dd hh:mm:ss")
receiptLayout = QFormLayout()
receiptLayout.addRow("Book:", QLabel(str(book.title)))
self.receiptDialog.setWindowTitle("Borrow Receipt")
self.receiptDialog.setLayout(receiptLayout)
receiptLayout.addRow("Book:", QLabel(book.title))
receiptLayout.addRow("Author:", QLabel(book.author))
receiptLayout.addRow("Borrower:", QLabel(name))
receiptLayout.addRow("Date borrowed:", QLabel(currentDateTimeString))
receiptLayout.addRow("Return deadline:", QLabel(deadlineString))
ok_button = QPushButton("OK")
ok_button.clicked.connect(self.receiptDialog.accept)
receiptLayout.addRow(ok_button)
self.receiptDialog.exec_()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
library = Library()
library.setStyleSheet("QTabBar::tab { height: 100px; transform: scale(5.5); min-width: 250px; font-size: 656px; }")
# Increase the height of the tabs
library.show()
sys.exit(app.exec_())
我试过 self.setIconSize(QtCore.QSize(220, 250)) 但这个只控制我不知道为什么的书的大小。
我也试过“self.tab_widget.setStyleSheet(”QTabBar::tab { max-width: 1500px; max-height: 1500px; }”)”但是这个根本没有改变任何东西。
我尝试了变换和宽度和高度,但没有运气 “library.setStyleSheet("QTabBar::tab { height: 100px; transform: scale(5.5); min-width: 250px; font-size: 656px; }") “这个只对选项卡有效,对图标无效
任何可以帮助我的 python 导出?
我遇到的另一个问题是,当我点击这本书并输入我的名字时,我的应用程序崩溃并自动关闭