- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 389字
- 2021-06-24 18:35:13
Defining getter and setter methods
In our previous section, we needed to know the value of a button in a given row and column of the button matrix for a given pattern. If the value was True, we colored the button green. If the value was False, we colored it in an alternative color.
We can get the value of the button by calling this line of code:
self.all_patterns[self.current_pattern.get()]['is_button_clicked_list'][row][col]
Notice how this line has four sets of square brackets, []. Since this nested super-scripting business can soon get ugly, we encapsulated this logic in a method named get_button_value(row, col). Now, whenever we need to get a button's value, we can simply call this method with the right parameters.
Now our code will not be littered with those ugly nested superscripts. Whenever we want to get the value of a button, we can call the get_button_value(row, col) method, which has a nice indicative name for the work it does. Isn't this much more readable and comprehensible than its rather ugly counterpart?
One thing is for sure: all logic that we build from now onward will heavily rely on data we get from, or set to, our data structure. Given that we will need all this data all the time in our program, let's write its getter and setter methods in advance. This will certainly make our lives a lot easier.
The goal for this part of the iteration is simple—to define getter and setter methods for all the data that we have decided to store in our data structure.
The code is as follows (see code 3.04.py):
def get_current_pattern_dict(self):
return self.all_patterns[self.current_pattern_index]
def get_bpu(self):
return self.get_current_pattern_dict()['bpu']
def set_bpu(self):
self.get_current_pattern_dict()['bpu'] = int(self.bpu_widget.get())
def get_number_of_units(self):
return self.get_current_pattern_dict()['number_of_units']
def set_number_of_units(self):
self.get_current_pattern_dict()['number_of_units']
= int(self.number_of_units_widget.get())
def get_list_of_drum_files(self):
return self.get_current_pattern_dict()['list_of_drum_files']
def get_drum_file_path(self, drum_index):
return self.get_list_of_drum_files()[drum_index]
def set_drum_file_path(self, drum_index, file_path):
self.get_list_of_drum_files()[drum_index] = file_path
def get_is_button_clicked_list(self):
return self.get_current_pattern_dict()['is_button_clicked_list']
def set_is_button_clicked_list(self, num_of_rows, num_of_columns):
self.get_current_pattern_dict()['is_button_clicked_list']
= [[False] * num_of_columns for x in range(num_of_rows)]
That is all there is to coding the getter and setter methods. The code should be self-explanatory if you have understood the underlying data structure, as all that we do here is either get a value or set a value for various items in the data structure.
With these methods now handy, let's complete coding the functionality of widgets we had earlier left uncoded.
- 零基礎學C++程序設計
- Java從入門到精通(第5版)
- Full-Stack Vue.js 2 and Laravel 5
- iOS應用逆向工程(第2版)
- Learning Hunk
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- Oracle 18c 必須掌握的新特性:管理與實戰
- jQuery Mobile移動應用開發實戰(第3版)
- 用戶體驗可視化指南
- 物聯網系統架構設計與邊緣計算(原書第2版)
- Kotlin進階實戰
- RESTful Web API Design with Node.js
- 可視化H5頁面設計與制作:Mugeda標準教程
- C語言從入門到精通(第5版)
- 快速搞定Spring Boot+Vue全棧開發