- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 370字
- 2021-06-24 18:35:12
Setting up the GUI in OOP
The text editor we developed in the previous chapter was implemented in procedural code. Although it offered some benefits for quick coding, it had some typical limitations:
- We started encountering global variables
- The function definitions needed to be defined above the code that called them
- Most importantly, the code was not reusable
Therefore, we need some way to ensure that our code is reusable. This is why programmers prefer to use object-oriented programming (OOP) to organize their code into classes.
OOP is a programming paradigm that shifts the focus onto the objects we want to manipulate rather than the logic required to manipulate them. This is in contrast to procedural programming, which views a program as a logical procedure that takes input, processes it, and produces some output.
OOP provides several benefits, such as data abstraction, encapsulation, inheritance, and polymorphism. In addition, OOP provides a clear modular structure for programs. Code modification and maintenance are easy, as new objects can be created without modifying the existing ones.
Let's build our drum program using OOP to illustrate some of these features. An indicative OOP structure for our drum program could be as follows (code 3.01.py):
from tkinter import Tk
PROGRAM_NAME = ' Explosion Drum Machine '
class DrumMachine:
def __init__(self, root):
self.root = root
self.root.title(PROGRAM_NAME)
if __name__ == '__main__':
root = Tk()
DrumMachine(root)
root.mainloop()
The description of the code is as follows:
- We create a class structure called DrumMachine and initialize the Toplevel window passed as an argument to it
- If the script is run as a standalone program, that is, if __name__ == '__main__', a new Tk() root object is created and the root window is passed as an argument to the DrumMachine object
- We then initiate an object from the DrumMachine class to get a Toplevel window
Now that we have our Toplevel window ready, let's stop adding any more visual elements and think about something that is critical to how well our program will eventually turn out to be. Let's spend some time finalizing the data structure for our program.
- Getting Started with React
- The Android Game Developer's Handbook
- HTML5 移動Web開發(fā)從入門到精通(微課精編版)
- HBase從入門到實(shí)戰(zhàn)
- 數(shù)據(jù)結(jié)構(gòu)與算法JavaScript描述
- MATLAB實(shí)用教程
- MATLAB定量決策五大類問題
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第2版)(微課版)
- Java程序設(shè)計(jì)入門
- 蘋果的產(chǎn)品設(shè)計(jì)之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗(yàn)的七個(gè)原則
- RocketMQ實(shí)戰(zhàn)與原理解析
- 多媒體技術(shù)及應(yīng)用
- Python Deep Learning
- Functional Python Programming
- 虛擬現(xiàn)實(shí)建模與編程(SketchUp+OSG開發(fā)技術(shù))