创建任意数量的按钮

2024-10-02 00:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在我开始之前先弄清楚。你知道吗

我的问题和这个问题一样:Tkinter: creating an arbitrary number of buttons/widgets

但这个公认的答案对我不适用。你知道吗

这是我的密码:

import os
try:
    from tkinter import *
except ImportError:
    from Tkinter import *

from subprocess import call

root = Tk()
root.wm_attributes("-fullscreen", "true")
root.config(background = "#FFFFFF")

backIMG = PhotoImage(file="b.gif")
usbIMG = PhotoImage(file="u.gif")
usbAuswahlIMG = PhotoImage(file="ua.gif")
downloadsIMG = PhotoImage(file="d.gif")
downloadsAuswahlIMG = 
PhotoImage(file="da.gif")

menuFrame = Frame(root, width=200, height=600, bg="#FFFF00")
menuFrame.grid(row=0, column=0, padx=10, pady=3)

def goBack():
    #os.system('python gui.py')
    root.destroy()

def selectUSB():
    downloadsButton.config(image=downloadsIMG)
    usbButton.config(image=usbAuswahlIMG)



def selectDownloads():
    usbButton.config(image=usbIMG) 
    downloadsButton.config(image=downloadsAuswahlIMG)
    i = 0
    buttons = dict()
    for file in os.listdir('/home/pi/Downloads'):
        if file.endswith(".mp3") or file.endswith(".wav"):
            buttons[i] = Button(contentFrame, text=file, width=60, font=("Sans", 15), command=lambda a=i: playDL(a).grid(row=i, column=0))
            i = i + 1
            print()

def playDL(index):
    print (index)

usbButton = Button(menuFrame, image=usbIMG, 
command=selectUSB)
usbButton.pack()

downloadsButton = Button(menuFrame, image=downloadsIMG, 
command=selectDownloads)
downloadsButton.pack()

stopButton = Button(menuFrame, image=backIMG, 
command=goBack)
stopButton.pack()

contentFrame = Frame(root, width=760, height=594, bg='#FFFFFF')
contentFrame.grid(row=0, column=1, padx=13, pady=3)

root.mainloop()

在另一篇帖子中,有人建议在这里添加以下代码:

buttons = dict()
for k in range(len(info)):
    buttons[k] = Button(top, text=info[k], command=lambda a=k: my_function(buttons[a]))

我已经这样做了,并在这里和那里改变它,以适应我的代码。你知道吗

问题是现在这些按钮根本不会出现在内容框上。它仍然在循环中运行,但没有看到按钮。一开始我有这样的想法:

for file in os.listdir('/home/pi/Downloads'):
        if file.endswith(".mp3") or file.endswith(".wav"):
            Button(contentFrame, text=file, width=60, font=("Sans", 15), command=lambda: playDL(i).grid(row=i, column=0))
            i = i + 1

所有的按钮都是一样的,但都是看得见的。你知道吗

我昨天开始用python编程,但我对Java和visualc非常熟悉,所以我对自己的工作有了基本的了解


Tags: imageimportconfigosbuttonrootwidthgif
1条回答
网友
1楼 · 发布于 2024-10-02 00:21:07

更改此项:

for file in os.listdir('/home/pi/Downloads'):
        if file.endswith(".mp3") or file.endswith(".wav"):
            Button(contentFrame, text=file, width=60, font=("Sans", 15),
            command=lambda: playDL(i).grid(row=i, column=0))
            i = i + 1

对此:

for file in os.listdir('/home/pi/Downloads'):
        if file.endswith(".mp3") or file.endswith(".wav"):
            Button(contentFrame, text=file, width=60, font=("Sans", 15),
            command=lambda i=i: playDL(i)).grid(row=i, column=0)
            i = i + 1

您将网格管理放在lambda函数内部,而不是按钮小部件外部。你知道吗

正如tobias在注释中指出的那样,您需要在lambda中使用i=i,以便每个按钮的值都是准确的,否则所有按钮都将具有循环中的最后一个值。你知道吗

相关问题 更多 >

    热门问题