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 8491137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:19:39+00:00 2026-06-10T22:19:39+00:00

How to do the same in PyQt4 as the function in this youtube video

  • 0

How to do the same in PyQt4 as the function in this youtube video tutorial.

There is a code but I need on pressed button digit to write the digit in a QlineEdit – for example as in this calculator:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

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

    def initUI(self):
        numbers = [ '7', '8', '9',
            '4', '5', '6',
            '1', '2', '3',
                    '*', '0', '#',]
        grid = QtGui.QGridLayout()
        j = 0
        pos = [(0, 0), (0, 1), (0, 2),
                (1, 0), (1, 1), (1, 2),
                (2, 0), (2, 1), (2, 2),
                (3, 0), (3, 1), (3, 2),
              ]
        for i in numbers:
            button = QtGui.QPushButton(i)
            grid.addWidget(button, pos[j][0], pos[j][1])
            j = j + 1
        self.setLayout(grid)   
        self.move(300, 150)
        self.setWindowTitle('Calculator')    
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
  • 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-10T22:19:41+00:00Added an answer on June 10, 2026 at 10:19 pm

    You need to connect the button to a click event and here is the updated code which works like that video 🙂

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from functools import partial
    from PyQt4 import QtGui, QtCore
    
    class Example(QtGui.QWidget):
        def __init__(self):
            super(Example, self).__init__()
            self.initUI()
    
        def initUI(self):
            numbers = [ '7', '8', '9',
                '4', '5', '6',
                '1', '2', '3',
                        '*', '0', '#',]
            self.vert_lay = QtGui.QVBoxLayout()
            self.dig_out = QtGui.QLineEdit()
            self.vert_lay.addWidget(self.dig_out)
            grid = QtGui.QGridLayout()
            j = 0
            pos = [(0, 0), (0, 1), (0, 2),
                    (1, 0), (1, 1), (1, 2),
                    (2, 0), (2, 1), (2, 2),
                    (3, 0), (3, 1), (3, 2),
                  ]
            for i in numbers:
                button = QtGui.QPushButton(i)
                grid.addWidget(button, pos[j][0], pos[j][1])
                j = j + 1
                button.clicked.connect(partial(self.addNumber,i))
    
            self.vert_lay.addLayout(grid)
            self.setLayout(self.vert_lay)  
    
            self.move(300, 150)
            self.setWindowTitle('Calculator')    
            self.show()
    
        def addNumber(self,num):
            current_txt = self.dig_out.text()
            self.dig_out.setText("%s%s" % (current_txt,num))
    
    def main():
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a blog editor using pyqt4. My issue is this. There is a
I have this sample of code: import sys from PyQt4.QtGui import (QApplication, QHBoxLayout, QVBoxLayout,
Same question for Windows Server 2003. Do I need to direct people to install
The same way i use this to detect when user scolled down the whole
This problem is same as asked in here . Given a list of coins,
Why this code show me a window without image? I try to use QByteArray
This question was originally asked in askubuntu.com but got no attention so I think
I am using pyQt4 and want to have a Browse button in my GUI
Same situation as this question, HttpListener not receiving remote requests, even with the firewall
I have some code creating a QTabWidget from Python using PyQt4. I want to

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.