Pyside6: Create QTabWidget with function rather than class

2024/11/20 7:13:57

I've been trying to make an application using Pyside6 and can't seem to understand why we can't create a QDialog having QTabWidget with just functions. I am not sure if I am making a mistake somewhere so, here is the code I've wrote:

Imports:

from PySide6.QtWidgets import (QMessageBox, QApplication, QWidget, QGridLayout, QLabel, QMainWindow, QTabWidget,QVBoxLayout)
from sys import argv

2 Tabs function:

def WelPage():grid = QGridLayout()wel_tab = QWidget()wel_tab.setLayout(grid)lab_name = QLabel("This is a label")git_link = QLabel("This is a link")git_link.setOpenExternalLinks(True)yt_link = QLabel("Another Link")yt_link.setOpenExternalLinks(True)grid.addWidget(lab_name, 0, 1)grid.addWidget(git_link, 1, 0)grid.addWidget(yt_link, 1, 3)def AboutPage():about_tab = QWidget()lo = QVBoxLayout()purpose = QLabel("A really long label")lo.addWidget(purpose)about_tab.setLayout(lo)

And the main function:

def main():w = QWidget()layout = QVBoxLayout()tw = QTabWidget()w.resize(450, 250)w.setWindowTitle('Window Title')layout.addWidget(tw)tw.addTab(WelPage(), "Welcome Screen")tw.addTab(AboutPage(), "About")tw.show()w.setLayout(layout)w.show()app.exec()if __name__ == "__main__":app = QApplication(argv)main()

Output: SC of Blank canvas

All this does is render a blank Dialog. Not sure why that is. Why must I be forced to use a class rather than this method?

Answer

You seem to lack certain basics of how widgets work in Qt.
QWidgets are (generally) supposed to have a parent. So when you instantiate a child widget, you pass to the constructor the parent. This way Qt knows how that they should be displayed. I recommend you follow some tutorials on how to do basic GUIs with Qt in Python.

Then, because you will need to know the parent in the function, either you can use a global variable or pass it as a parameter to the function, like so :

def add_about_widget(parent):about_widget = QWidget(parent)  # <--- here !...

If you want to have menu items, I recommend you use a QMenuBar. Also, Qt nicely create a window for you when you ask it to show() your main widget, but explicitly creating the main window makes things clearer in my opinion.

https://en.xdnf.cn/q/118445.html

Related Q&A

Pythons End Of Life

What exactly will happen to Python 2.7 after 1/2020?I understand that Python 2.7 will no longer be supported but what will actually happen? Does it mean that decision makers will delete the whole cod…

Gathering numerical data from a string input

I would like to get user input for their credit rating e.g AAA, A, BBB etc and then assign an interest rate to this. For example, if the user has a good credit rating e.g AAA I would charge an interest…

Getting Turtle in Python to recognize click events [duplicate]

This question already has an answer here:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates(1 answer)Closed 5 months ago.Im trying to make Connect …

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …

Counting unique words

Question:Devise an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as !, ?, ., , , : and ; and …

Django cant find template dir?

Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the sa…

Python Programming Loop

Im doing an assignment where I have to conduct a quiz for different topics. This is my code so far.print("Hello and welcome to Shahaads quiz!") #Introduction name = input("What is your n…

How to fix stale element error without refreshing the page

Trying to get details of Tyres on this page. https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL . Each tyre has different FINITIONS. The price and other details are different for each FINITIONS. I w…

Fastest way in numpy to get distance of product of n pairs in array

I have N number of points, for example: A = [2, 3] B = [3, 4] C = [3, 3] . . .And theyre in an array like so: arr = np.array([[2, 3], [3, 4], [3, 3]])I need as output all pairwise distances in BFS (Bre…

How to get argument to ignore part of message

I just wondering how to get the if statement(if 0 < int(message.content)< 153:) to only test part of the message, not the full message.content. Eg: if I put in 1s 100, I want it to test if ONLY t…