Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:53:24+00:00 2026-06-18T08:53:24+00:00

I have the mainwindow in the ‘DataClass’.How can I create widgets in another class(the

  • 0

I have the mainwindow in the ‘DataClass’.How can I create widgets in another class(the HelloClass)

test.py

import sys
import label
from PyQt4 import QtGui, QtCore

class DataClass(QtGui.QMainWindow):
    def __init__(self):
        super(DataClass, self).__init__()
        self.window()

    def window(self):
        ex=label.HelloClass(self)
        ex.print_label()

def main():
    app = QtGui.QApplication(sys.argv)
    ob=DataClass()
    ob.show()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()

and this is the ‘label.py’ file:

import sys
from PyQt4 import QtGui, QtCore

class HelloClass(QtGui.QMainWindow):

    def print_label(self):
        self.la=QtGui.QLabel("hello",self)
        self.la.move(300,100)
        self.la.show()

import sys
from PyQt4 import QtGui, QtCore

class HelloClass(QtGui.QMainWindow):

    def print_label(self):
        self.la=QtGui.QLabel("hello",self)
        self.la.move(300,100)
        self.la.show()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:53:25+00:00Added an answer on June 18, 2026 at 8:53 am

    You can’t have two QMainWindow class, you should just not inherit from QMainWindow on HelloClass. And if you are setting parent to label then set it your DataClass which is your QMainWindow.

    class HelloClass(object):
        def print_label(self, parent):
            self.la = QtGui.QLabel("hello", parent)
            self.la.move(300, 100)
            self.la.show()
    
    class DataClass(QtGui.QMainWindow):
        def __init__(self):
            super(DataClass, self).__init__()
            self.window()
    
        def window(self):
            ex = label.HelloClass()
            ex.print_label(self)
    

    But to be honest, the best way to create GUI using PyQt is to use QtDesigner. Create your .ui file with QtDesigner and then create .py file with command pyuic4 your.ui -o ui_your.py.

    — UPDATE —

    Your controller class for using gui created by QtDesigner would look like this:

    from ui_objects import Ui_Objects  # this is class created with QtDesigner, name of class is a 'Ui_' + name of main Object in QtDesigner    
    
    class Objects(QtGui.QWidget):
    
        def __init__(self):
            QtGui.QWidget.__init__(self)
            self.ui = Ui_Objects()
            self.ui.setupUi(self)
    
        # then you can add your own code, in example connect your own methods to actions for widgets
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following dependency property in my MainWindow class (inherits from WPF's Window)
I have the following code: <Window x:Class=kkk.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <Window.Resources> <Style
i have a window which is like this <Window x:Class=pharmacy_Concept.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
I have one MainWindow.xib file. I have one label and one button on first
I have a mainwindow, and another window called Loginwindow. In some point this LoginWindow
I have an application in which i have a mainwindow.ui file and i create
In a simple WPF, I have renamed MainWindow class to MyClass and I got
In my Application i have a MainWindow that import over MEFedMVVM the ViewModel: xmlns:mefed=clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF
I have a MainWindow (form1) and a class called Module In Module there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.