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

  • SEARCH
  • Home
  • 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 9021209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:12:06+00:00 2026-06-16T05:12:06+00:00

I am trying to create a custom widget with a button and a label

  • 0

I am trying to create a custom widget with a button and a label using the code:

from PySide.QtGui import *
from PySide.QtCore import *

class MyWidget(QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.initUI()
    def initUI(self):
        self.btn = QPushButton('dia', self)
        self.btn.move(20, 20)
        self.le = QLineEdit(self)
        self.le.move(100, 20)

Following this, I create a class for my main frame and try to display this widget with the code:

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.mw = MyWidget()
        self.mw.move(20, 20)

        self.resize(250, 300)
        self.center()
        self.setWindowTitle('Custom Widget Example')
        self.show()

When I run this script from the terminal, it shows but its blank. I do not see my widget anywhere.

What do I need to do to get this widget showing?

Thank you.

  • 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-16T05:12:08+00:00Added an answer on June 16, 2026 at 5:12 am

    Why are you using coordinates? Use layouts:

    class MyWidget(QWidget):
        def __init__(self, parent=None):
            super(MyWidget, self).__init__(parent)   #always provide parent!
            self.initUI()
        def initUI(self):
            self.btn = QPushButton('dia', self)
            self.le = QLineEdit(self)
            layout = QHBoxLayout()  #whatever layout you want
            layout.addWidget(self.btn)
            layout.addWidget(self.le)
            self.setLayout(layout)
    
    
    class Example(QWidget):
        def __init__(self, parent=None):
            super(Example, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            self.mw = MyWidget(self)   # remember the parent!
            layout = QHBoxLayout()
            layout.addWidget(self.mw)
            self.setLayout(layout)
    
            self.resize(250, 300)
            self.center()
            self.setWindowTitle('Custom Widget Example')
            self.show()
    

    If you want finer control over the disposition of the elements use a grid layout.

    If you really want to use “absolute coordinates” then you must always remember to pass the parent widget, otherwise children widgets will not have a place to draw themselves.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a custom pin code widget for android as an
I am trying to create a custom Dojo ValidationIdBox widget that inherits from ValidationTextBox.
I am trying to create a custom popup view that can be called from
I'm trying to create a custom usercontrol that acts like a button, but i
I'm a jQuery newb and I've been trying to create a custom slideshow widget
Trying to create a widget with custom fonts, but read that widget doesn't support
I'm trying to create a custom Dojo widget for my application. <!-- Dojo widget
I'm trying to create a custom field for a ModelForm. I'm extending from ModelMultipleChoiceField
I'm trying to create a custom tweet button with a popup, this part works.
I'm trying to create a 3 state toggle button by overriding the Button class.

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.