Python 如何使用Listbox中的每个新选择更新tkinter标签?

Python 如何使用Listbox中的每个新选择更新tkinter标签?,python,class,user-interface,tkinter,Python,Class,User Interface,Tkinter,我希望在选择列表框中的名称时,右侧的标签会更新。选择列表中的新名称后,右侧的名称标签保持不变 我尝试将name\u list.index(ACTIVE)直接放在student\u name.set()中作为student\u name.set(student\u list[name\u list.index(ACTIVE)]。get\u name())。恐怕它只是使用索引0,甚至没有使用活动选择 from tkinter import * # Defining Student class cl

我希望在选择列表框中的名称时,右侧的标签会更新。选择列表中的新名称后,右侧的名称标签保持不变

我尝试将
name\u list.index(ACTIVE)
直接放在student\u name.set()中作为
student\u name.set(student\u list[name\u list.index(ACTIVE)]。get\u name())
。恐怕它只是使用索引0,甚至没有使用活动选择

from tkinter import *

# Defining Student class
class Student:

  def __init__(self, name, instrument=""):
    self.name = name
    self.instrument = instrument

  def get_name(self):
    return self.name

  def get_instrument(self):
    return self.instrument

# Creating Student objects and list
s1 = Student("Seth", "Trumpet")
s2 = Student("Cassie", "Flute")
s3 = Student("Cody", "Guitar")
students_list = [s1, s2, s3]

# GUI created
def make_window():
  rootWindow = Tk()
  rootWindow.title("Instructor Database Application")
  rootWindow.grid()

  frame0 = Frame(rootWindow)
  frame0.grid(column=0, row=0, padx=20, pady=20)

  name_list = Listbox(frame0, height=10)
  name_list.grid(column=0, row=0)
  for Student in students_list:
    name_list.insert(END, Student.get_name())

  frame1 = Frame(rootWindow)
  frame1.grid(column=1, row=0, padx=20, pady=20)

  student_index = name_list.index(ACTIVE)
  student_name = StringVar()
  student_name.set(students_list[student_index].get_name())

  name_label = Label(frame1, textvariable=student_name)
  name_label.grid(column=0, row=0)

  instrument_label = Label(frame1, text="Instrument")
  instrument_label.grid(column=0, row=1)

  return rootWindow

def main():
  app = make_window()
  app.mainloop()

main()

程序以名字开头,无法更改。有什么建议吗?

您必须将函数绑定到
列表框
,当您更改选择时将执行该列表框。此函数必须更改标签中的文本

listbox.bind('<<ListboxSelect>>', my_function)
listbox.bind(“”,my_函数)

最小工作示例

import tkinter as tk

# --- function ---

def on_selection(event):
    # here you can get selected element
    print('previous:', listbox.get('active'))
    print(' current:', listbox.get(listbox.curselection()))

    # or using `event`

    #print('event:', event)
    #print('widget:', event.widget)
    print('(event) previous:', event.widget.get('active'))
    print('(event)  current:', event.widget.get(event.widget.curselection()))

    lbl['text'] = "Seleced: " + listbox.get(listbox.curselection())
    print('---')

# --- main ---

root = tk.Tk()

listbox = tk.Listbox(root)
listbox.pack()

listbox.insert(1, 'Hello 1')
listbox.insert(2, 'Hello 2')
listbox.insert(3, 'Hello 3')
listbox.insert(4, 'Hello 4')
listbox.bind('<<ListboxSelect>>', on_selection)

lbl = tk.Label(root, text='?')
lbl.pack()

root.mainloop()
将tkinter作为tk导入
#---功能---
def on_选择(事件):
#在这里您可以获得选定的元素
打印('previous:',listbox.get('active'))
打印('current:',listbox.get(listbox.curselection()))
#或使用`事件`
#打印('事件:',事件)
#打印('widget:',event.widget)
打印('(事件)上一个:',event.widget.get('active'))
打印(“(事件)当前:”,event.widget.get(event.widget.curselection())
lbl['text']=“Seleced:+listbox.get(listbox.curselection())
打印('--')
#---梅因---
root=tk.tk()
listbox=tk.listbox(根)
listbox.pack()
插入(1,“Hello 1”)
插入(2,‘Hello 2’)
插入(3,‘Hello 3’)
插入(4,‘Hello 4’)
listbox.bind(“”,在选择时)
lbl=tk.Label(根,文本=“?”)
lbl.pack()
root.mainloop()

您必须将函数绑定到
列表框
,当您更改选择时,该列表框将被执行。此函数必须更改标签中的文本

listbox.bind('<<ListboxSelect>>', my_function)
listbox.bind(“”,my_函数)

最小工作示例

import tkinter as tk

# --- function ---

def on_selection(event):
    # here you can get selected element
    print('previous:', listbox.get('active'))
    print(' current:', listbox.get(listbox.curselection()))

    # or using `event`

    #print('event:', event)
    #print('widget:', event.widget)
    print('(event) previous:', event.widget.get('active'))
    print('(event)  current:', event.widget.get(event.widget.curselection()))

    lbl['text'] = "Seleced: " + listbox.get(listbox.curselection())
    print('---')

# --- main ---

root = tk.Tk()

listbox = tk.Listbox(root)
listbox.pack()

listbox.insert(1, 'Hello 1')
listbox.insert(2, 'Hello 2')
listbox.insert(3, 'Hello 3')
listbox.insert(4, 'Hello 4')
listbox.bind('<<ListboxSelect>>', on_selection)

lbl = tk.Label(root, text='?')
lbl.pack()

root.mainloop()
将tkinter作为tk导入
#---功能---
def on_选择(事件):
#在这里您可以获得选定的元素
打印('previous:',listbox.get('active'))
打印('current:',listbox.get(listbox.curselection()))
#或使用`事件`
#打印('事件:',事件)
#打印('widget:',event.widget)
打印('(事件)上一个:',event.widget.get('active'))
打印(“(事件)当前:”,event.widget.get(event.widget.curselection())
lbl['text']=“Seleced:+listbox.get(listbox.curselection())
打印('--')
#---梅因---
root=tk.tk()
listbox=tk.listbox(根)
listbox.pack()
插入(1,“Hello 1”)
插入(2,‘Hello 2’)
插入(3,‘Hello 3’)
插入(4,‘Hello 4’)
listbox.bind(“”,在选择时)
lbl=tk.Label(根,文本=“?”)
lbl.pack()
root.mainloop()

mainloop
mainloop
之前的所有代码都会在您看到窗口之前执行,并且只执行一次。您必须绑定到列表函数,该函数将在每次选择列表中的元素时执行,并且该函数必须更改标签。@furas OK。我理解这个概念。当我进行新的选择时,您对我应该在何处或如何调用此函数有何建议?我不确定如何检测到新的选择。看起来有点像是一项工作,但我想这会有用的。谢谢你在我的回答中举了个例子。它显示了如何绑定在您选择新元素时可以更改值的函数。
mainloop
之前的所有代码都会在您看到窗口之前执行,并且只执行一次。您必须绑定到列表函数,该函数将在每次选择列表中的元素时执行,并且该函数必须更改标签。@furas OK。我理解这个概念。当我进行新的选择时,您对我应该在何处或如何调用此函数有何建议?我不确定如何检测到新的选择。看起来有点像是一项工作,但我想这会有用的。谢谢你在我的回答中举了个例子。它显示了如何绑定在选择新元素时可以更改值的函数。GitHub上tkinter(和其他python模块)的更多示例:GitHub上tkinter(和其他python模块)的更多示例: