Tkinterウィンドウでラベルを動的に追加/削除/更新するにはどうすればよいですか?
Tkinter Labelウィジェットを使用して、テキストと画像を表示できます。ラベルウィジェットを構成することで、ウィジェットのテキスト、画像、その他のプロパティを動的に変更できます。
ラベルウィジェットを動的に更新するには、 config(** options)のいずれかを使用できます またはインライン構成方法 テキストの更新などには、 Label ["text"] =text;を使用できます。 ラベルウィジェットを削除するには、 pack_forget()を使用できます メソッド。
例
# Import the required libraries
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")
def add_label():
global label
label=Label(win, text="1. A Newly created Label", font=('Aerial 18'))
label.pack()
def remove_label():
global label
label.pack_forget()
def update_label():
global label
label["text"]="2. Yay!! I am updated"
# Create buttons for add/remove/update the label widget
add=ttk.Button(win, text="Add a new Label", command=add_label)
add.pack(anchor=W, pady=10)
remove=ttk.Button(win, text="Remove the Label", command=remove_label)
remove.pack(anchor=W, pady=10)
update=ttk.Button(win, text="Update the Label", command=update_label)
update.pack(anchor=W, pady=10)
win.mainloop() 上記のコードを実行すると、いくつかのボタンが含まれるウィンドウが表示されます。各ボタンを使用して、アプリケーションのラベルを更新/削除または追加できます。
出力
[ラベルの更新]ボタンをクリックすると、ラベルは次のように更新されます-
-
Tkinterボタンを動的に生成する方法は?
この記事では、tkinterウィンドウでボタンを動的に作成する方法を説明します。ボタンを動的に作成するということは、ボタンにイベントを追加してボタンとその機能をカスタマイズすることを意味します。 まず、ノートブックにtkinterライブラリをインポートし、次にボタンを使用してインスタンスを作成します ウィンドウの親やルートなどのパラメータを受け取る関数、各ボタンやコマンドで割り当てる値であるtextvariable。 構文 Button(parent, textvariable, command) 例 from tkinter import * import tkinter as tk #
-
tkinterウィンドウを閉じるにはどうすればよいですか?
tkinterを使用してアプリケーションを作成するのは簡単ですが、タイトルバーのボタンを使用せずにウィンドウまたはフレームを閉じることが困難になる場合があります。このような場合、 .destroy()を使用できます ウィンドウを閉じる方法。 tkinter属性は互いに独立しているため、ボタンを使用してウィンドウを閉じる別のメソッドを作成できます。 例 #Import the library from tkinter import * #Create an instance of window win = Tk() #Set the geometry of the window win.g