这是我第一次在这里发帖,这么赤裸裸的和我在一起!我正在开发一个应用程序,里面有一个足球运动员的列表框。我想弄清楚的是,我想双击listbox
中的一个项目,我想让它在点击时显示在搜索条目中。
这是我的代码。我已经尝试绑定按钮点击和创建一个新的功能来尝试和查看在搜索框中选择的项目。我看过一些视频,试图弄清楚这一点,但他们都没有OOP,所以也许我把这些keybinds
或函数放在错误的地方。我只是想能够双击我的listbox
中的一个项目,并有该名称出现在搜索栏中。
import re
import tkinter as tk
from tkinter import ttk
from tkinter import *
from PIL import Image, ImageTk
root = tk.Tk()
class Player:
"""Creating the information for the soccer player"""
def __init__(self,name,team,number):
self.name = name
self.team = team
self.number = number
class Screen:
def __init__(self,master):
# Creating the main tkinter window
self.master = master
self.search_player()
def search_player(self):
"""The main search window, this is where you will search for the player"""
string1 = tk.StringVar()
# Clears first frame
for i in self.master.winfo_children():
i.destroy()
# Create first frame
self.frame1 = tk.Frame(self.master, width=500, height=300)
self.frame1.pack()
# Search Label
self.search_label = tk.Label(self.master, text="Search Player:", font=("Times, 12"))
self.search_label.place(x=15, y=50)
# Entry for searching
self.search_entry = tk.Entry(self.master, font=("arial, 15"),textvariable=string1)
self.search_entry.place(x=125, y=50)
# Search Button
self.search_button = tk.Button(self.master, text="Search", font=("Times, 13"), command=lambda: self.player_info_screen())
self.search_button.place(x=360, y=47)
# Listbox storing players name
self.listbox1 = tk.Listbox(self.master,font=("Times, 12"))
self.listbox1.place(x=125, y=100)
# Scroll bar for list box
self.scrollbar = tk.Scrollbar(self.master)
self.scrollbar.place(x=310, y=100)
# Attaching listbox to scrollbar and scrollbar to listbox
self.listbox1.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox1.yview())
# list made up of each object instance showing players name
footballer_names = [player1.name, player2.name, player3.name, player4.name]
# Adding players into listbox
for name in footballer_names:
self.listbox1.insert(0, name)
player1 = Player("Neymar","Paris Saint Germain","10")
player2 = Player("Lionel Messi","Inter Miami","30")
player3 = Player("Kevin De Bruyne","Manchester City","17")
player4 = Player("Cristiano Ronaldo", "Al Nassr", "7")
window = Screen(root)
root.geometry("500x400")
root.mainloop()
2条答案
按热度按时间watbbzwu1#
我想双击列表框中的一个项目,我希望它在点击时显示在搜索条目中。
这个问题是可以解决的。
self
添加到self.string1
print_selection
的函数。self.listbox1.place
后添加self.listbox1.bind
代码:
截图:
fivyi3re2#
您可以在labels/search字段上设置文本标志,以返回函数而不是默认字符串,这样您就可以创建一个默认返回“SearchPlayer”的函数,然后在双击该播放器时将返回值更改为播放器名称