TkinterPythonの折りたたみ可能なペイン
TkinterはPythonのGUI構築ライブラリです。この記事では、折りたたみ可能なペインを作成する方法を説明します。 GUIキャンバス上に大量のデータを表示する必要があるが、常に表示したくない場合に便利です。折りたたみ可能になっているため、必要に応じて表示できます。
以下のプログラムは、矢印を拡大および縮小した後の結果を表示する折りたたみ可能なペインを作成します。コードコメントは、各ステップで採用するアプローチを示しています。
例
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *
class cpane(ttk.Frame):
def __init__(self, MainWindow, expanded_text, collapsed_text):
ttk.Frame.__init__(self, MainWindow)
# The class variable
self.MainWindow = MainWindow
self._expanded_text = expanded_text
self._collapsed_text = collapsed_text
# Weight=1 to grow it's size as needed
self.columnconfigure(1, weight=1)
self._variable = tk.IntVar()
# Creating Checkbutton
self._button = ttk.Checkbutton(self, variable=self._variable,
command=self._activate, style="TButton")
self._button.grid(row=0, column=0)
# Create a Horizontal line
self._separator = ttk.Separator(self, orient="horizontal")
self._separator.grid(row=0, column=1, sticky="we")
self.frame = ttk.Frame(self)
# Activate the class
self._activate()
def _activate(self):
if not self._variable.get():
# Remove this widget when button pressed.
self.frame.grid_forget()
# Show collapsed text
self._button.configure(text=self._collapsed_text)
elif self._variable.get():
# Increase the frame area as needed
self.frame.grid(row=1, column=0, columnspan=2)
self._button.configure(text=self._expanded_text)
def toggle(self):
self._variable.set(not self._variable.get())
self._activate()
# Creating root window or MainWindow
root = Tk()
root.geometry('300x300')
# Creating Object of Collapsible Pane Container
cpane_obj = cpane(root, 'Close Me', 'Open Me!')
cpane_obj.grid(row=0, column=0)
# Buttons to # appear in collapsible pane
b = Button(cpane_obj.frame, text=" Frame Expanded").grid(
row=1, column=2, pady=20)
b = Checkbutton(cpane_obj.frame, text="Hi There ! How are you doing?").grid(
row=3, column=4, pady=20)
mainloop() 出力
上記のコードを実行すると、次の結果が得られます-
-
PythonTkinterのメソッドの後
TkinterはGUIを作成するためのPythonライブラリです。 GUIウィンドウやその他のウィジェットを作成および操作してデータやGUIイベントを表示するための多くの組み込みメソッドがあります。この記事では、afterメソッドがTkinterGUIでどのように使用されるかを見ていきます。 構文 .after(delay, FuncName=FuncName) This method calls the function FuncName after the given delay in milisecond ウィジェットの表示 ここでは、単語のリストをランダムに表示するフレームを作成しま
-
Pythonでの継承
この記事では、Python3.xでの継承と拡張クラスについて学習します。またはそれ以前。 継承は実際の関係をうまく表し、再利用性を提供し、推移性をサポートします。開発時間が短縮され、メンテナンスが容易になり、拡張も容易になります。 継承は大きく5つのタイプに分類されます- シングル 複数 階層的 マルチレベル ハイブリッド 上の図に示されているように、継承とは、実際に親クラスのオブジェクトを作成せずに、他のクラスの機能にアクセスしようとするプロセスです。 ここでは、単一の階層型継承の実装について学習します。 単一継承 例 # parent class class Studen