Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Tkinter border control Frame

Shulou Source: shulou.com Published: 2022-06-01 03:35:45 09月26日 Update

This article mainly explains the "Tkinter frame control Frame how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Tkinter frame control Frame how to use" it!

Among the many controls, the border control Frame can be said to be a special one. The reason for this is that the Frame control itself is not displayed, but only to organize other controls into a whole for layout management.

The above video is used in the previous description of the Text control. The grid layout was used at that time, and to ensure that a sufficient number of buttons were placed, the columnspan of the Text control was set to 8:

# create text widget.text = Text (root, undo=True, background= "# a0ffa0", foreground= "# 000000", height = 10) text.grid (row=2, column=0, columnspan=8)

But the downside to this is that the layout of the Text control is affected by the number of buttons, which is not good. This article uses the Frame control to solve this problem.

First build the Frame control that contains the first line of buttons:

Edit_frame = Frame (root)

# change state function.def change_state (): state= text.cget ('state') if state=='disabled': text.config (state='normal') text.config (background='#a0ffa0') else: text.config (state='disabled') text.config (background='#efefef') # change state button.eb = Button (edit_frame,text= "Enable", width=8, command=change_state) eb.grid (row=0, column=0, sticky=E+W)

# delete selection.def delete_selection (): try: sel_from = text.index (SEL_FIRST) sel_to = text.index (SEL_LAST) # delete the selection. Text.delete (sel_from, sel_to) except TclError: pass

# delete selection button.db = Button (edit_frame,text= "Delete", width = 8, command=delete_selection) db.grid (row=0, column=1, sticky=E+W)

# undo buttonundo = Button (edit_frame, text='Undo', width = 8, command=lambda:text.edit_undo ()) undo.grid (row=0, column = 2, sticky=E+W)

# redo buttonredo = Button (edit_frame, text='Redo', width = 8, command=lambda:text.edit_redo ()) redo.grid (row=0, column = 3, sticky=E+W)

Edit_frame.grid (row=0, column=0, sticky=W)

The code is long, but most of the content is the same as that in the Text control article, except for two points:

The code at the beginning and end of this code adds code to build the edit_frame control and layout the control using the grid method, respectively.

The parent control of all buttons has been changed from root to edit_frame.

The next format buttons are handled in the same way:

Format_frame = Frame (root)

# create fontsfonts = [Font (family='SimHei', size=20, weight=BOLD), Font (family='SimHei', size=16), Font (family='SimSun', size=12, weight=BOLD), Font (family='SimSun', size=12)]

# delete selection.def format (index): tag_name = 'Format' + str (index) try: sel_from = text.index (SEL_FIRST) sel_to = text.index (SEL_LAST) for name in text.tag_names (): text.tag_remove (name, sel_from, sel_to) text.tag_add (tag_name, sel_from, sel_to) # set format at first time. Range_count = len (text.tag_ranges (tag_name)) if range_count = = 2: text.tag_config (tag_name, font=fonts [index]) except TclError: pass

# delete selection button.for i in range (0,4): fb = Button (format_frame, text= "Format" + str (I), width = 8, command=lambda vicii: format (v)) fb.grid (row=1, column=i, sticky=E+W)

Format_frame.grid (row=1, column=0, sticky=W)

After using the Frame control, when you layout the root window, the four edit buttons and the four format buttons participate as two whole, so you no longer need to consider the number of buttons when you generate the Text control:

# create text widget.text = Text (root, undo=True, background= "# a0ffa0", foreground= "# 000000", height = 10) text.grid (row=2, column=0) Thank you for your reading. This is the content of "how to use Tkinter frame Control Frame". After the study of this article, I believe you have a deeper understanding of how to use Tkinter frame control Frame. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Controls buttons borders layout content code learning that is quantity whole format say problem different same and then no one line not good two. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft MariaDB MySQL Shulou Tech Info