Just pointing out a very minor commenting mistake. In the first example with a text box, the comment says "# Create calendar" instead of "# Create textbox"
Frank•Fri, 29 May 2015
Thanks Max! I updated it :-)
Matt•Tue, 23 Jun 2015
I apologize if you have answered this in the tutorial, but how would you display text on the window? For example, if I wanted to make a simple calculator that let the user input two numbers and display the sum, what would I use to display the sum?
Frank•Tue, 23 Jun 2015
Hi Matt, it depends on how you want to display the text on the window. If you want to display in a textbox you could use the .setText() method. A textbox is often used in calculators.
import sys from PyQt4.QtCore import pyqtSlot from PyQt4.QtGui import *
# create our window app = QApplication(sys.argv) w = QWidget() w.setWindowTitle('Textbox example @pythonspot.com')
Hope that helps.If you have any more questions let me know :-)
Matt•Wed, 24 Jun 2015
That does exactly what I wanted, thank you.
Matt•Fri, 26 Jun 2015
What is the best way to get the size of a widget? I would like to place a button to the right of a combobox I've placed. I'm adding items to the box from a file, so I don't know the length of the strings it holds ahead of time. As the box expands to fit the length of its items I also don't know its size, but I would still like to be able to set the space between the button and the box.
I've tried using combobox.iconSize(), which appears to return a different value than the size of the box.
Resizing components with components next to it does not seem to be a standard thing for a Qt to do. The code below is a bit of a workaround. The example below shows a combobox with an initial width. When the button is pressed a new item is added and the combobox is resized.
#! /usr/bin/env python # -*- coding: utf-8 -*- # import sys from PyQt4.QtGui import * from PyQt4.QtCore import pyqtSlot from PyQt4.QtCore import QTimer import time
# Create an PyQT4 application object. a = QApplication(sys.argv)
# The QWidget widget is the base class of all user interface objects in PyQt4. w = QMainWindow()
# Set window size. w.resize(340, 100)
# Set window title w.setWindowTitle("PyQT Python Widget!")
# Action when button is pressed @pyqtSlot() def on_click(): startWidth = combo.size().width() combo.addItem("This is a very long element") combo.resize(combo.size()) combo.update() combo.repaint() qApp.processEvents() moveButton()
# Create a QTimer. This timer will be a watchdog # that checks if the combobox has resized. timer = QTimer() timer.timeout.connect(moveButton) timer.start(10)
Leave a Reply:
Just pointing out a very minor commenting mistake. In the first example with a text box, the comment says "# Create calendar" instead of "# Create textbox"
Thanks Max! I updated it :-)
I apologize if you have answered this in the tutorial, but how would you display text on the window? For example, if I wanted to make a simple calculator that let the user input two numbers and display the sum, what would I use to display the sum?
Hi Matt, it depends on how you want to display the text on the window. If you want to display in a textbox you could use the .setText() method.
A textbox is often used in calculators.
Output:
<img src="https://pythonspot.com/wp-c..." alt="PyQT Textbox"/>
The same principle works with a label:
<img src="https://pythonspot.com/wp-c..." alt="QT Label"/>
Hope that helps.If you have any more questions let me know :-)
That does exactly what I wanted, thank you.
What is the best way to get the size of a widget? I would like to place a button to the right of a combobox I've placed. I'm adding items to the box from a file, so I don't know the length of the strings it holds ahead of time. As the box expands to fit the length of its items I also don't know its size, but I would still like to be able to set the space between the button and the box.
I've tried using combobox.iconSize(), which appears to return a different value than the size of the box.
Hi Matt, you can get the size of a widget using:
Resizing components with components next to it does not seem to be a standard thing for a Qt to do. The code below is a bit of a workaround. The example below shows a combobox with an initial width. When the button is pressed a new item is added and the combobox is resized.
Output:
<img src="https://pythonspot.com/wp-c..." alt="PyQT"/>
After click:
<img src="https://pythonspot.com/wp-c..." alt="PyQT"/>
Hope that helps. Let me know if you have any questions
Thank you!