我的页码显示有错误,我知道原因和位置,但我不知道如何解决(PyQt)

问题描述 投票:-1回答:1

我正在制作一个漫画查看器,但我遇到了bug.所以我做了一个程序,每当我们点击 "文件 "然后 "测试 "时就会打开一个新的标签。

所以我做了一个程序,每次我们点击 "文件",然后点击 "测试",就会打开一个新的标签页。

当我们点击第一个标签页上的左右按钮时,我们所在的页面就会显示出来(例如 "第3页,共23页")然而,问题是当我们打开新的标签页时,页码的显示有问题。在前几个标签页中,页码相互重叠,只有最后打开的标签页显示正确。

我知道原因了。当我们打开一个新的标签页时,我们会经过 Open(self) 功能打开一个标签页,并 self.pageNumDisp = QLabel("Page : 0 of "+str(len(self.image_list)-1)) 在上一个标签页的页码显示上打印它的文本,然后将其添加到新的标签页。

问题是我不知道如何解决这个错误,非常欢迎你的帮助。

其实我在使用self.prepagebutton.setEnabled(TrueFalse)的时候也遇到过类似的问题,最后我只是在书本处于第一页和最后一页的时候没有禁用按钮,而是直接重新开始书本,但实际问题只是让我确认了问题所在。

这里是添加新标签、创建页码列表和设置第0页显示的部分。

def Open(self):
    n = str(self.n)
    self.i[n] = 0
    self.tab[n] = QWidget()
    self.tab[n].objectName()
    self.tab[n].layout = QGridLayout()
    self.bookNo = self.bookNo + 1

    self.image_list = []

    for i in range(1,randint(3,50)):
        self.image_list.append(str(i))

    self.whichList[self.bookNo] = self.image_list

    self.pageNumDisp = QLabel("Page : 0 of "+str(len(self.image_list)-1))
    self.tab[n].layout.addWidget(self.pageNumDisp,1, 1)

    self.nextpagebutton = QPushButton(">", self)
    self.nextpagebutton.setObjectName('>{}'.format(self.n))
    self.nextpagebutton.setFixedSize(50, 780)
    self.tab[n].layout.addWidget(self.nextpagebutton, 0, 2)
    self.nextpagebutton.clicked.connect(self.readnext)

    self.prevpagebutton = QPushButton("<", self)
    self.prevpagebutton.setObjectName('<{}'.format(self.n))
    self.prevpagebutton.setFixedSize(50, 780)
    self.tab[n].layout.addWidget(self.prevpagebutton, 0, 0)
    self.prevpagebutton.clicked.connect(self.readprev)

    self.index[n] = self.tabs.addTab(self.tab[n], "Tab " + str(self.n))
    self.tab[n].setLayout(self.tab[n].layout)
    self.n = int(self.n) + 1
    print(self.i)

这里是改变页码显示的部分,这取决于我们是按右键还是左键。

def readnext(self):
    name = str(self.sender().objectName())
    identifier = name[-1]
    self.i[identifier] += 1
    whichComic = str(self.tabs.currentIndex())

    if self.i[identifier] == -1:
        self.i[identifier] = len(self.whichList.get(int(whichComic)))-1
    if self.i[identifier] == len(self.whichList.get(int(whichComic))):
        self.i[identifier] = 0

    self.pageNumDisp.setText("")
    self.pageNumDisp.setText("Page : "+str(self.i[identifier])+" of "+str(len(self.whichList.get(int(whichComic)))-1))
    self.tab[identifier].layout.addWidget(self.pageNumDisp, 1, 1)

    print(self.i)

def readprev(self):
    name = str(self.sender().objectName())
    identifier = name[-1]
    self.i[identifier] -= 1
    whichComic = str(self.tabs.currentIndex())

    if self.i[identifier] == -1:
        self.i[identifier] = len(self.whichList.get(int(whichComic)))-1
    if self.i[identifier] == len(self.whichList.get(int(whichComic))):
        self.i[identifier] = 0

    self.pageNumDisp.setText("")
    self.pageNumDisp.setText("Page : "+str(self.i[identifier])+" of "+str(len(self.whichList.get(int(whichComic)))-1))
    self.tab[identifier].layout.addWidget(self.pageNumDisp, 1, 1)

    print(self.i)

这里有我的完整程序,你可以试试,它的工作原理。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QPushButton, QAction, QStatusBar, QToolBar, QTabWidget
from PyQt5.QtCore import QCoreApplication
from random import randint


class FenetrePrincipale(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Liseuse de Bandes Dessinées")
        self.left = 10
        self.top = 10
        self.width = 900
        self.height = 850
        self.i = 0

        self.setGeometry(self.left, self.top, self.width, self.height)

        self.toolbar = QToolBar("Toolbar")
        self.layout = QGridLayout()
        self.setCentralWidget(QWidget(self))
        self.centralWidget().setLayout(self.layout)
        self.move(200, 0)
        self.addToolBar(self.toolbar)

        self.table_widget = MyTableWidget()

        self.open = QAction("test", self)
        self.open.triggered.connect(self.table_widget.Open)

        self.setStatusBar(QStatusBar())
        self.menuFichier = self.menuBar().addMenu("&File")
        self.menuFichier.addAction(self.open)

        self.widget = QWidget()
        self.widget.setLayout(self.layout)
        self.setCentralWidget(self.widget)

        self.setCentralWidget(self.table_widget)

        self.show()


class MyTableWidget(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.layout = QGridLayout()
        self.i = {}
        self.n = 0
        self.index = {}
        self.tab = {}
        self.whichList = {}
        self.bookNo = -1

        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tabs.setDocumentMode(True)
        self.tabs.setMovable(True)

        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)

    def Open(self):
        n = str(self.n)
        self.i[n] = 0
        self.tab[n] = QWidget()
        self.tab[n].objectName()
        self.tab[n].layout = QGridLayout()
        self.bookNo = self.bookNo + 1

        self.image_list = []

        for i in range(1,randint(3,50)):
            self.image_list.append(str(i))

        self.whichList[self.bookNo] = self.image_list

        self.pageNumDisp = QLabel("Page : 0 of "+str(len(self.image_list)-1))
        self.tab[n].layout.addWidget(self.pageNumDisp,1, 1)

        self.nextpagebutton = QPushButton(">", self)
        self.nextpagebutton.setObjectName('>{}'.format(self.n))
        self.nextpagebutton.setFixedSize(50, 780)
        self.tab[n].layout.addWidget(self.nextpagebutton, 0, 2)
        self.nextpagebutton.clicked.connect(self.readnext)

        self.prevpagebutton = QPushButton("<", self)
        self.prevpagebutton.setObjectName('<{}'.format(self.n))
        self.prevpagebutton.setFixedSize(50, 780)
        self.tab[n].layout.addWidget(self.prevpagebutton, 0, 0)
        self.prevpagebutton.clicked.connect(self.readprev)

        self.index[n] = self.tabs.addTab(self.tab[n], "Tab " + str(self.n))
        self.tab[n].setLayout(self.tab[n].layout)
        self.n = int(self.n) + 1
        print(self.i)

    def readnext(self):
        name = str(self.sender().objectName())
        identifier = name[-1]
        self.i[identifier] += 1
        whichComic = str(self.tabs.currentIndex())

        if self.i[identifier] == -1:
            self.i[identifier] = len(self.whichList.get(int(whichComic)))-1
        if self.i[identifier] == len(self.whichList.get(int(whichComic))):
            self.i[identifier] = 0

        self.pageNumDisp.setText("")
        self.pageNumDisp.setText("Page : "+str(self.i[identifier])+" of "+str(len(self.whichList.get(int(whichComic)))-1))
        self.tab[identifier].layout.addWidget(self.pageNumDisp, 1, 1)

        print(self.i)

    def readprev(self):
        name = str(self.sender().objectName())
        identifier = name[-1]
        self.i[identifier] -= 1
        whichComic = str(self.tabs.currentIndex())

        if self.i[identifier] == -1:
            self.i[identifier] = len(self.whichList.get(int(whichComic)))-1
        if self.i[identifier] == len(self.whichList.get(int(whichComic))):
            self.i[identifier] = 0

        self.pageNumDisp.setText("")
        self.pageNumDisp.setText("Page : "+str(self.i[identifier])+" of "+str(len(self.whichList.get(int(whichComic)))-1))
        self.tab[identifier].layout.addWidget(self.pageNumDisp, 1, 1)

        print(self.i)


app = QCoreApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = FenetrePrincipale()
window.show()
python class user-interface pyqt tabs
1个回答
0
投票

问题解决了..:

import sys
from PyQt5.QtWidgets import QApplication, QStackedLayout, QMainWindow, QLabel, QGridLayout, QWidget, QPushButton, QAction, QStatusBar, QToolBar, QTabWidget
from PyQt5.QtCore import QCoreApplication
from random import randint


class FenetrePrincipale(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Liseuse de Bandes Dessinées")
        self.left = 10
        self.top = 10
        self.width = 900
        self.height = 850
        self.i = 0

        self.setGeometry(self.left, self.top, self.width, self.height)

        self.toolbar = QToolBar("Toolbar")
        self.layout = QGridLayout()
        self.setCentralWidget(QWidget(self))
        self.centralWidget().setLayout(self.layout)
        self.move(200, 0)
        self.addToolBar(self.toolbar)

        self.table_widget = MyTableWidget()

        self.open = QAction("test", self)
        self.open.triggered.connect(self.table_widget.Open)

        self.setStatusBar(QStatusBar())
        self.menuFichier = self.menuBar().addMenu("&File")
        self.menuFichier.addAction(self.open)

        self.widget = QWidget()
        self.widget.setLayout(self.layout)
        self.setCentralWidget(self.widget)

        self.setCentralWidget(self.table_widget)

        self.show()

class MyBookWidget(QWidget):
    def __init__(self, pages, parent = None):
        super().__init__(parent)


        grid_layout = QGridLayout(self)
        self.nextpagebutton = QPushButton(">", self)
        self.nextpagebutton.setFixedSize(50, 720)
        grid_layout.addWidget(self.nextpagebutton, 0, 2)
        self.nextpagebutton.clicked.connect(self.readnext)

        self.prevpagebutton = QPushButton("<", self)
        self.prevpagebutton.setFixedSize(50, 720)
        grid_layout.addWidget(self.prevpagebutton, 0, 0)
        self.prevpagebutton.clicked.connect(self.readprev)

        # adding a label for displaying the current page number
        self.pageNumDisp = QLabel()
        grid_layout.addWidget(self.pageNumDisp, 1, 1)

        # Here I use a stacked layout for showing the pages of the book
        # navigating between pages can then be done by changing the index of the layout 
        self.page_viewer = QStackedLayout()
        grid_layout.addLayout(self.page_viewer, 0, 1)

        for n in pages:
            page = QLabel()
            page.setText(str(n))
            self.page_viewer.addWidget(page)

        # connect a slot to the currentChanged signal of the QStackedWidget for updating the label
        self.page_viewer.currentChanged.connect(self.page_changed)
        self.page_changed(0)

    def page_changed(self, page_nr):
        self.pageNumDisp.setText("Page : "+str(page_nr+1)+" of "+str(self.page_count()))

    def page_count(self):
        return self.page_viewer.count()

    def current_page(self):
        return self.page_viewer.currentIndex()

    def readnext(self):
        new_page  = min(self.current_page()+1, self.page_count()-1)
        self.page_viewer.setCurrentIndex(new_page)

    def readprev(self):
        new_page = max(self.current_page() - 1, 0)
        self.page_viewer.setCurrentIndex(new_page)

class MyTableWidget(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.layout = QGridLayout()

        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tabs.setDocumentMode(True)
        self.tabs.setMovable(True)

        # Add tabs to widget
        self.layout.addWidget(self.tabs, 0, 0)
        self.setLayout(self.layout)

    def book_count(self):
        return self.tabs.count()

    def Open(self):
        image_list = []
        for i in range(1, randint(3, 50)):
            image_list.append(str(i))
        widget = MyBookWidget(image_list)
        self.tabs.addTab(widget, "Book"+str(self.book_count()))


app = QCoreApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = FenetrePrincipale()
window.show()

app.exec_()
© www.soinside.com 2019 - 2024. All rights reserved.