Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Creating a multiple Selection using Tkinter
Next article icon

A collapsible pane, as the name suggests, is a pane which can be collapsed. User can expand pane so that they can perform some task and when task is completed, pane can be collapsed. 
In Tkinter, Collapsible pane is a container with an embedded button-like control which is used to expand or collapse this container.
Prerequisites: 
 

Frame ClassCheckbutton ClassStyling in widgetsconfigure() method


CollapsiblePane class - 
CollapsiblePane widget is used to store any other widgets inside of it. It can be toggled on or off, so widgets inside of it aren't always shown. 
 

Parameters: 
parent = The parent of the widget. 
expanded_text = The text shown on the Button when the pane is open. 
collapsed_text = The text shown on the Button when the pane is closed.
Functions: 
_activate() = Checks the value of variable and shows or hides the Frame. 
toggle() = Switches the LabelFrame to the opposite state.
Widgets: 
self_button = The Button that toggles the Frame. 
frame = The Frame that holds the widget. 
_separator = The Separator.


 

Python3
# Implementation of Collapsible Pane container# importing tkinter and ttk modulesimporttkinterastkfromtkinterimportttkclassCollapsiblePane(ttk.Frame):"""     -----USAGE-----    collapsiblePane = CollapsiblePane(parent,                          expanded_text =[string],                          collapsed_text =[string])    collapsiblePane.pack()    button = Button(collapsiblePane.frame).pack()    """def__init__(self,parent,expanded_text="Collapse <<",collapsed_text="Expand >>"):ttk.Frame.__init__(self,parent)# These are the class variable# see a underscore in expanded_text and _collapsed_text# this means these are private to classself.parent=parentself._expanded_text=expanded_textself._collapsed_text=collapsed_text# Here weight implies that it can grow it's# size if extra space is available# default weight is 0self.columnconfigure(1,weight=1)# Tkinter variable storing integer valueself._variable=tk.IntVar()# Checkbutton is created but will behave as Button# cause in style, Button is passed# main reason to do this is Button do not support# variable option but checkbutton doself._button=ttk.Checkbutton(self,variable=self._variable,command=self._activate,style="TButton")self._button.grid(row=0,column=0)# This will create a separator# A separator is a line, we can also set thicknessself._separator=ttk.Separator(self,orient="horizontal")self._separator.grid(row=0,column=1,sticky="we")self.frame=ttk.Frame(self)# This will call activate function of classself._activate()def_activate(self):ifnotself._variable.get():# As soon as button is pressed it removes this widget# but is not destroyed means can be displayed againself.frame.grid_forget()# This will change the text of the checkbuttonself._button.configure(text=self._collapsed_text)elifself._variable.get():# increasing the frame area so new widgets# could reside in this containerself.frame.grid(row=1,column=0,columnspan=2)self._button.configure(text=self._expanded_text)deftoggle(self):"""Switches the label frame to the opposite state."""self._variable.set(notself._variable.get())self._activate()

  
Program to demonstrate use of CollapsiblePane - 
 

Python3
# Importing tkinter and ttk modulesfromtkinterimport*fromtkinter.ttkimport*# Importing Collapsible Pane class that we have# created in separate filefromcollapsiblepaneimportCollapsiblePaneascp# Making root window or parent windowroot=Tk()root.geometry('200x200')# Creating Object of Collapsible Pane Container# If we do not pass these strings in# parameter the default strings will appear# on button that were, expand >>, collapse <<cpane=cp(root,'Expanded','Collapsed')cpane.grid(row=0,column=0)# Button and checkbutton, these will# appear in collapsible pane containerb1=Button(cpane.frame,text="GFG").grid(row=1,column=2,pady=10)cb1=Checkbutton(cpane.frame,text="GFG").grid(row=2,column=3,pady=10)mainloop()

Output: 
 


 


 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp