Why Tkinter can be procedural and PyQt can only be used just with the OO paradigm? Why Tkinter can be procedural and PyQt can only be used just with the OO paradigm? tkinter tkinter

Why Tkinter can be procedural and PyQt can only be used just with the OO paradigm?


I can only speak about tkinter, but both modes are useful. Procedural code is useful for exploring with minimal boilerplate. For instance, after running the following from an Idle editor (intentionally without a mainloop() call)

from tkinter import *from tkinter.font import Fontroot = Tk()spin = Spinbox(root, from_=0, to=9, width=3,               font=Font(family='Helvetica', size=36, weight='bold'))spin.pack()

I can interactively experiment with changing the attributes of spin, such as by entering

>>> spin['fg'] = 'red'>>> spin['fg'] = 'blue'>>> spin['justify'] = 'right'

However, for a finished app of any complexity, I think it better to put gui setup code in a one or more methods of one or more classes. For instance, Idle, a tkinter app, is composed of numerous classes that either subclass a widget or contain a widget.


I thank all of you for helping me understand the errors in my understanding.

For any others who are introduced to PyQt (after coming from a tkinter background) and read tutorials that state that PyQt demands an OOP approach, the following two identical programs show that this is simply not the case (although, clearly, to build big apps, an OOP approach is advisable).

Procedural PyQt:

import sysfrom PyQt5.QtWidgets import (QWidget, QToolTip,    QPushButton, QApplication)from PyQt5.QtGui import QFontapp = QApplication(sys.argv)w = QWidget()QToolTip.setFont(QFont('SansSerif', 10))w.setToolTip('This is a <b>QWidget</b> widget')btn = QPushButton('Button', w)btn.setToolTip('This is a <b>QPushButton</b> widget')btn.resize(btn.sizeHint())btn.move(50, 50)w.setGeometry(300, 300, 300, 200)w.setWindowTitle('Tool Tips')w.show()sys.exit(app.exec_())

OOP PyQt:

import sysfrom PyQt5.QtWidgets import (QWidget, QToolTip,    QPushButton, QApplication)from PyQt5.QtGui import QFontclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        QToolTip.setFont(QFont('SansSerif', 10))        self.setToolTip('This is a <b>QWidget</b> widget')        btn = QPushButton('Button', self)        btn.setToolTip('This is a <b>QPushButton</b> widget')        btn.resize(btn.sizeHint())        btn.move(50, 50)        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle('Tooltips')        self.show()app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())

Thank you all for your comments and helping me realise my stupidity.