-1

The code below is a WebBrowser code. The problem is when I want to switch tabs which happens in the function 'SwitchTab'. I've checked that in function 'SwitchTab', my 'Tab_Content' variable is empty-as it prints 'None'- and here is the problem. I couldn't figure out why would that be empty.(I've Bolded the partS of the code which stands for the Issue) Thanks In Advance...

P.S: I'm using Python3.8 and PyCharm4.5.4 and Also PyQt5

import sys
import os
import json

from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout,
                             QHBoxLayout, QPushButton, QLabel,
                             QLineEdit, QTabBar, QFrame, QStackedLayout, QTabWidget)

from PyQt5.QtGui import QIcon, QWindow, QImage
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *




class AddressBar(QLineEdit):
     def __init__(self):
        super().__init__()

     def mousePressEvent(self, e):
         self.selectAll()


class App(QFrame):
    def __init__(self):
        super().__init__()

        self.CreateApp()
        self.setWindowTitle("Web Browser")
        self.setBaseSize(500 , 500)

    def CreateApp(self):

        self.layout = QVBoxLayout()
        self.layout.setSpacing(0)
        self.layout.setContentsMargins(0,0,0,0)

        *#Creating Tabs*
        self.TabBar = QTabBar(movable=True, tabsClosable=True)
        self.TabBar.tabCloseRequested.connect(self.TabClose)
        **self.TabBar.tabBarClicked.connect(self.SwitchTab)**

        self.TabBar.setCurrentIndex(0)

        *#Tabs List*
        self.TabCount = 0
        self.Tabs = []

        *#Creating the AddressBar*
        self.ToolBar = QWidget()
        self.ToolBarLayout = QHBoxLayout()
        self.AddressBar = AddressBar()

        self.ToolBar.setLayout(self.ToolBarLayout)
        self.ToolBarLayout.addWidget(self.AddressBar)

        *#NewTab Button*
        self.AddTabButton = QPushButton("+")
        self.AddTabButton.clicked.connect(self.AddTab)

        self.ToolBarLayout.addWidget(self.AddTabButton)

        *#Main View*
        self.Container = QWidget()
        self.Container.layout = QStackedLayout()
        self.Container.setLayout(self.Container.layout)


        self.layout.addWidget(self.TabBar)
        self.layout.addWidget(self.ToolBar)
        self.layout.addWidget(self.Container)

        self.setLayout(self.layout)
        
        self.AddTab()
        self.show()

    def TabClose(self, i):
        self.TabBar.removeTab(i)
        print(self.TabCount)
        print(self.Tabs)

    def AddTab(self):
        i = self.TabCount

        self.Tabs.append(QWidget())
        self.Tabs[i].layout = QVBoxLayout()
        self.Tabs[i].setObjectName("Tab " + str(i))

        *#Generate WebView*
        self.Tabs[i].content = QWebEngineView()
        self.Tabs[i].content.load(QUrl.fromUserInput("http://google.com"))

        *#Add WebView to Tabs layout*
        self.Tabs[i].layout.addWidget(self.Tabs[i].content)

        *#Set top level tab [] to layout*
        self.Tabs[i].setLayout(self.Tabs[i].layout)

        *#Add tab to top level StackedWidget*
        self.Container.layout.addWidget(self.Tabs[i])
        self.Container.layout.setCurrentWidget(self.Tabs[i])

        *#Set the tab at top of screen*
        self.TabBar.addTab("New Tab - " + str(i))
        self.TabBar.setTabData(i, "tab" + str(i))
        self.TabBar.setCurrentIndex(i)
        self.TabCount += 1

    **def SwitchTab(self, i):
        Tab_Data = self.TabBar.tabData(i)
        print("tab: ", Tab_Data)
        Tab_Content = self.findChild(QWidget, Tab_Data)
        print(Tab_Content)
        self.Container.layout.setCurrentWidget(Tab_Content)**


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = App()

    sys.exit(app.exec_())
  • 1
    Please do not add asterisks to try to "format" the code, use comments instead (or include the code between
    tags and use actual tags). Your code must be readable as *pure* code, and possibly it can be copied/pasted/run as it is. Please edit your question to remove unnecessary characters, and ensure its syntax is correct.
    – musicamante Aug 31 '20 at 09:33

1 Answers1

0

Because the findChild method takes the objectName into it, not the tabData you set. in this case, the solution is simple. Just set the objectName and the tabData with the same parameter.

self.TabBar.setTabData(i, "Tab " + str(i))
self.Tabs[i].setObjectName("Tab " + str(i))
Mustafa AHCI
  • 169
  • 2
  • 12