Python
 Computer >> コンピューター >  >> プログラミング >> Python

Tkinterでリストボックスアイテムを編集するにはどうすればよいですか?


Tkinterリストボックスウィジェットは通常、アイテムのリストを作成するために使用されます。数字や文字のリストを保存し、リストアイテムの選択や編集などの多くの機能をサポートできます。

リストボックスアイテムを編集するには、最初に listbox.curselection()を使用してループ内のアイテムを選択する必要があります。 機能し、リストボックス内の前のアイテムを削除した後、新しいアイテムを挿入します。リストボックスに新しいアイテムを挿入するには、 listbox.insert(** items)を使用できます。 機能。

この例では、リストボックスウィジェットにアイテムのリストを作成し、ボタンを使用してリストで選択したアイテムを編集します。

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Create a Listbox widget
lb = Listbox(win, width=100, height=10, background="purple2", foreground="white", font=('Times 13'), selectbackground="black")

lb.pack()

# Select the list item and delete the item first
# Once the list item is deleted,
# we can insert a new item in the listbox
def edit():
   for item in lb.curselection():
      lb.delete(item)
      lb.insert("end", "foo")

# Add items in the Listbox
lb.insert("end", "item1", "item2", "item3", "item4", "item5")

# Add a Button To Edit and Delete the Listbox Item
ttk.Button(win, text="Edit", command=edit).pack()

win.mainloop()

出力

上記のコードを実行すると、リストアイテムを選択して編集できます。

Tkinterでリストボックスアイテムを編集するにはどうすればよいですか?

[編集]ボタンをクリックすると、アイテムのリストを構成できます。

Tkinterでリストボックスアイテムを編集するにはどうすればよいですか?


  1. Tkinterのリストボックスで選択した複数のアイテムを削除するにはどうすればよいですか?

    TkinterでListboxメソッドを使用してリストボックスを作成し、このリストから複数の選択されたアイテムを削除したいとします。 リストボックスから複数のリストを選択するには、 selectmodeを使用します。 複数として 。リストを繰り返し処理して、いくつかのボタンを使用して削除操作を実行できます。 例 #Import the required libraries from tkinter import * #Create an instance of tkinter frame or window win= Tk() #Set the geometry win.geometry

  2. 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