python—tkinter中对列表框的关注

xriantvc  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(212)

这是我的节目。我有很多问题,因为我不知道如何使用一个类,所以我尽我所能。除了我的列表框在将其焦点设置为0时的特定时间具有焦点外,所有操作都“正常”运行。问题是,当我单击按钮时,会出现一个带有焦点的条目,这种情况会发生,但它并不总是具有焦点。我希望你能帮助我,并且你能理解我的计划。

from tkinter import *

f = Tk()
f.geometry("600x600+600+100")

### Reading files

# Types

types = open("files/types.txt").read().split()

# Moves

moves = open("files/all moves/names.txt").read().split("\n")

# Pokemons Stats

names = open("files/all stats/names.txt").read().split("\n")
t1 = open("files/all stats/type 1.txt").read().split("\n")
t2 = open("files/all stats/type 2.txt").read().split("\n")
ab1 = open("files/all stats/ability 1.txt").read().split("\n")
ab2 = open("files/all stats/ability 2.txt").read().split("\n")
ab3 = open("files/all stats/ability 3.txt").read().split("\n")
hp = open("files/all stats/hp.txt").read().split("\n")
atk = open("files/all stats/atk.txt").read().split("\n")
defense = open("files/all stats/def.txt").read().split("\n")
spa = open("files/all stats/spa.txt").read().split("\n")
spd = open("files/all stats/spd.txt").read().split("\n")
spe = open("files/all stats/spe.txt").read().split("\n")

# Regroupe all stats in 1 list

stats = []
stats.append(names)
stats.append(t1)
stats.append(t2)
stats.append(ab1)
stats.append(ab2)
stats.append(ab3)
stats.append(hp)
stats.append(atk)
stats.append(defense)
stats.append(spa)
stats.append(spd)
stats.append(spe)

### Lists

# Icons

icons = []
for i in types:
    icons.append(PhotoImage(file="files/types_icons/" + i + ".png"))

# Labels

labels = []

# Buttons

but = []

# Entry

entry = []

# Listbox

lbox = []

# StringVar

svar = []

### Functions

def pos1(x):

    lbox[x].delete(0, END)
    for i in stats[0]:
        lbox[x].insert(END, i)
    lbox[x].selection_clear(0, END)
    lbox[x].place(anchor="nw", x=4 + x * 320, y=82, width=273)

    entry[1-x].place_forget()
    lbox[1-x].place_forget()
    svar[2+x].set("")

    entry[x].place(anchor="nw", x=5 + x * 320, y=57, width=271, height=25)
    entry[x].focus_set()

def cancel(event):
    labels[1].focus_set()

    for i in lbox:
        i.place_forget()

    for i in entry:
        i.place_forget()

# Search a Pokemon

def search0(event):
    lbox[0].delete(0, END)

    no = []
    yes = []
    start = []

    for x in stats[0][-1::-1]:
        index = x
        add = 1
        letters = []
        for i in x:
            letters.append(i.lower())
        for i in svar[2].get():
            if i in letters:
                y = letters.index(i)
                del letters[y]
            else:
                add = 0

        if add == 1:
            if svar[2].get().lower() in index.lower():
                if index[0:len(svar[2].get())].lower() == svar[2].get().lower():
                    start.insert(0, index)
                else:
                    yes.insert(0, index)
            else:
                no.insert(0, index)
    for i in start:
        lbox[0].insert(END, i)
    for i in yes:
        lbox[0].insert(END, i)
    for i in no:
        lbox[0].insert(END, i)

def search1(event):
    lbox[1].delete(0, END)

    no = []
    yes = []
    start = []

    for x in stats[0][-1::-1]:
        index = x
        add = 1
        letters = []
        for i in x:
            letters.append(i.lower())
        for i in svar[3].get():
            if i in letters:
                y = letters.index(i)
                del letters[y]
            else:
                add = 0

        if add == 1:
            if svar[3].get().lower() in index.lower():
                if index[0:len(svar[3].get())].lower() == svar[3].get().lower():
                    start.insert(0, index)
                else:
                    yes.insert(0, index)
            else:
                no.insert(0, index)
    for i in start:
        lbox[1].insert(END, i)
    for i in yes:
        lbox[1].insert(END, i)
    for i in no:
        lbox[1].insert(END, i)

def select0(event):
    lbox[0].selection_clear(0, END)
    lbox[0].selection_set(lbox[0].nearest(event.y))

def select1(event):
    lbox[1].selection_clear(0, END)
    lbox[1].selection_set(lbox[1].nearest(event.y))

def choosed1(event):
    lbox[0].selection_clear(0, END)
    lbox[0].selection_set(lbox[0].nearest(event.y))
    svar[0].set(lbox[0].get(lbox[0].nearest(event.y)))
    lbox[0].place_forget()
    entry[0].place_forget()

def choosed2(event):
    lbox[1].selection_clear(0, END)
    lbox[1].selection_set(lbox[1].nearest(event.y))
    svar[1].set(lbox[1].get(lbox[1].nearest(event.y)))
    lbox[1].place_forget()
    entry[1].place_forget()

### Creating Labels, Buttons, StringVar, Entry and Listbox

labels.append(Label(f, text="Choose your pokemons :", justify="center", bg="white"))
labels.append(Label(f, text="Vs", justify="center"))

svar.append(StringVar())  # sv de l'entrée du pkm 1
svar[-1].set(stats[0][0])
but.append(Button(f, textvariable=svar[-1], justify="center", bg="white", command=lambda: pos1(0)))  # Pokemon 1
svar.append(StringVar())  # sv de l'entrée du pkm 1
svar[-1].set(stats[0][0])
but.append(Button(f, textvariable=svar[-1], justify="center", bg="white", command=lambda: pos1(1)))  # Pokemon 2

svar.append(StringVar())  # sv de l'entrée du pkm 1
svar[-1].set("")
entry.append(Entry(f, textvariable=svar[-1], justify="center", bg="white"))  # Entry Pokemon 1
svar.append(StringVar())  # sv de l'entrée du pkm 2
svar[-1].set("")
entry.append(Entry(f, textvariable=svar[-1], justify="center", bg="white"))  # Entry Pokemon 2

svar.append(StringVar())  # sv de la listbox du pkm 1
svar[-1].set(stats[0])
lbox.append(Listbox(f, listvariable=svar[-1], justify="center", bg="white", exportselection=0, activestyle="none", takefocus=0))  # Listbox Names 1
svar.append(StringVar())  # sv de la listbox du pkm 2
svar[-1].set(stats[0])
lbox.append(Listbox(f, listvariable=svar[-1], justify="center", bg="white", exportselection=0, activestyle="none", takefocus=0))  # Listbox Names 2

### Start

if __name__ == "__main__":
    labels[0].place(anchor="n", x=300, y=5, width=590)
    labels[1].place(anchor="n", x=300, y=32, width=40)
    but[0].place(anchor="nw", x=5, y=30, width=270)
    but[1].place(anchor="ne", x=595, y=30, width=270)

    while True:

        # Searching a pkm
        entry[0].bind("<KeyRelease>", search0)
        entry[1].bind("<KeyRelease>", search1)

        # Echap => close open bar
        f.bind("<Escape>", cancel)

        # Blue bg on cursor
        lbox[0].bind("<Motion>", select0)
        lbox[1].bind("<Motion>", select1)

        # Choose pkm
        lbox[0].bind("<Button-1>", choosed1)
        lbox[1].bind("<Button-1>", choosed2)

        try:
            f.update()
        except:
            break

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题