Tkinter 从列表中创建按钮,每个按钮都有自己的功能 [英] Tkinter create buttons from list each with its own function

查看:48
本文介绍了Tkinter 从列表中创建按钮,每个按钮都有自己的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表中创建按钮并根据列表项为每个按钮分配一个功能.我在下面试过,按钮没有响应点击.我看到使用 lambda 函数将参数传递给函数的解决方案,但我想要单独的函数.在 Anaconda 中使用 Python 3.5

I want to create buttons from a list and assign each button a function based on the list item. I tried below and the button doesn't respond to a click. I see solutions where a lambda function is used to pass a parameter to a function but I would like separate functions. Using Python 3.5 in Anaconda

import tkinter as tk

def North():
    print('slected North')

def South():
    print('slected South')

def East():
    print('slected East')

def West():
    print('slected West')

lst = ['North','South','East','West']
win = tk.Tk()
win.title = 'Compass'
for col,Direction in enumerate(lst):
        butName = tk.Button(win, text = Direction, command = Direction)
        butName.grid(row = 1, column = col)


win.mainloop()

推荐答案

您的列表包含字符串;它需要包含功能

Your list contains strings; it needs to contain functions

lst = [North,South,East,West]

这篇关于Tkinter 从列表中创建按钮,每个按钮都有自己的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆