Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python find pack in the source code?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "Python how to find pack in the source code". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "Python how to find pack in the source code" can help you solve the problem.

Foreword:

tkinter provides three layout management methods:

1、pack

2、grid

3、place

Each layout manager is very useful, according to different needs, choose the corresponding layout, each control can use pack as a layout manager, from the source code to see where pack ()?

HelloWorld see pack () import tkinter my_window = tkinter.Tk() my_window.geometry('400x200') hello_world_label = tkinter.Label(my_window, text="Hello World", bg='red') hello_world_label.pack() my_window.mainloop()

Code Description:

1. First import tkinter module

2. Create a Tk object that represents the currently applied window

3. Set the window size to 800 pixels wide and 200 pixels high

Create a Label object and place it in the currently applied window.

5. Call the pack () method of the Label object to display it

mainloop () starts the main loop

Output:

Why can every control call the pack () method? Let's go into the pack () method and see

II. Analysis of pack () method pack = configure = config = pack_configure

From ide click pack () method, see the source code above, you can see pack actually points to pack_configure, then we continue to pack_configure () method to see its implementation

III. Analysis of pack_configure () method def pack_configure(self, cnf={}, **kw): self.tk.call( ('pack', 'configure', self._ w) + self._ options(cnf, kw))

Since the underlying is based on the Tcl/Tk framework (1 script interpreter), the last call is the API provided by Tk.

If you look closely, you see that pack_configure is in a class called Pack, and the line just below is also in the Pack class.

pack = configure = config = pack_configure

pack is a class variable in the Pack class, and points to the pack_configure method object (note: in Python classes, each instance method belongs to an object of the method class, so it can still be assigned to other variables), so why can Label call methods in the Pack class? This relates to the inheritance structure of each control class in tkinter

IV. Inheritance Structure of Label

Pack class is the parent class of Label. The above figure shows the inheritance structure of Label control. There is no pack () method defined in Label. In this case, the pack () method will be looked up.

1. First look for pack () method in Label, and find none.

2. Look up the pack () method in the parent Widget class, and find that there is no pack () method yet.

3, continue to look up the parent BaseWidget pack () method, found that there is no

4. Then look for the pack () method in the parent class Pack, and finally find the pack attribute in the Pack class, and then call the pack attribute.

5. The pack_configure method object actually pointed to by the pack attribute

Finally, the pack_configure method object is called.

V. Check the harvest of pack () method source code

1. Learn the inheritance structure of Label

2. Learn the usage of multiple class variables pointing to the same method object. The advantage of doing so is for code compatibility.

pack = configure = config = pack_configure

In actual work, you can use pack or pack_configure, they are all the same, config and configure in the middle are not recommended, because they conflict with the config and configure methods of Label itself, and the config and configure methods that modify the attribute values of Label itself will be called first.

3. Check how pack () is used directly in the source code, without having to query the API documentation.

Keyword parameters available in all pack () methods

About "Python how to find pack in the source code" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report