Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为在tkinter中按下已创建按钮的次数创建计数器?_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 如何为在tkinter中按下已创建按钮的次数创建计数器?

Python 如何为在tkinter中按下已创建按钮的次数创建计数器?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,因此,我制作了一个简单的程序,允许我为按钮键入标签,并在tkinter gui中创建和单击它。现在我只需要添加一个函数,返回每个按钮被单击的次数。问题是我创建的按钮实际上没有在输入中编码,所以我发现很难做到这一点。我觉得我必须使用lambda函数,但我对它一点经验都没有。谢谢你的帮助 代码: 您需要使用lambda将输入的单词传递到按钮函数(): word_dict={} def按钮功能(word): 单词dict[word]+=1 打印(单词,单词,单词) def按钮_生成器(): #获取输入

因此,我制作了一个简单的程序,允许我为按钮键入标签,并在tkinter gui中创建和单击它。现在我只需要添加一个函数,返回每个按钮被单击的次数。问题是我创建的按钮实际上没有在输入中编码,所以我发现很难做到这一点。我觉得我必须使用lambda函数,但我对它一点经验都没有。谢谢你的帮助

代码:


您需要使用
lambda
将输入的单词传递到
按钮函数()

word_dict={}
def按钮功能(word):
单词dict[word]+=1
打印(单词,单词,单词)
def按钮_生成器():
#获取输入字
word=计数器\u entry.get().strip()
#确保输入的单词在字典中是唯一的
如果单词和单词不在单词目录中:
计数=len(单词)
按钮=tk.按钮(窗口,文本=单词,宽度=10,高度=2,fg=“红色”,
command=lambda w=word:button_function(w))#将输入字传递给button_function()
按钮位置(x=计数%5*116,y=计数//5*60)
word_dict[word]=0#初始化输入字的计数器
计数器项。删除(0,'end')

使用计数器标签更新代码:

word_dict={}
def按钮功能(word):
count=word\u dict[word].get()
word\u dict[word]。设置(计数+1)
def按钮_生成器():
word=计数器\u entry.get().strip()
如果单词和单词不在单词目录中:
计数=len(单词)
行,列=计数//5*2,计数%5
#创建word按钮
按钮=tk.按钮(窗口,文本=单词,宽度=10,高度=2,fg=“红色”,
命令=lambda w=word:button_函数(w))
网格(行=行,列=列,padx=10,pady=(10,0))
#创建相应的计数器标签
var=tk.IntVar()#用于计数器值
标签(窗口,textvariable=var).grid(行=行+1,列=列)
单词dict[word]=var
计数器项。删除(0,“结束”)

您的代码已经做到了这一点,对吗?您所要做的就是转储
word\u dict
数组。字典键与按钮标题匹配。换句话说,我想你已经写了这个程序了<代码>按钮。创建按钮后,配置(command=lambda btn=button:button\u function(btn))内部
button\u maker
。另外,请注意,
按钮\ u函数
必须带有一个参数。另一种方法是将
按钮功能
绑定到
事件。现在您不需要lambda函数,可以知道使用
单击了哪个按钮。widget
这里的参数是ur
button\u函数
参数。@Timrobts字典键匹配按钮标题是什么意思?我的代码中没有字典。
button\u函数
如何获取参数?为什么不使用
grid
。谢谢,这很有效。现在,我如何允许这个计数器在gui本身的按钮下发生?换句话说,当我按下gui中的一个按钮时,数字1会在下面弹出,然后每次单击都会递增?创建一个标签,然后在函数中创建它的文本,将文本作为
word\u dict[word]
@Lyon Dalipsy答案更新。@acw1668谢谢。
import tkinter as tk
from tkinter import *

window = tk.Tk()
window.title("Tkinter FINAL")
window.geometry("600x400")
window.resizable(width=False, height=False)
WIDTH = 800
HEIGHT = 600

counter_name = tk.Label(window, text="Counter Word", width=20)
counter_name.place(x=460,y=318)
counter_entry = tk.Entry(window, width=20)
counter_entry.place(x=470,y=338)

position_x = 0
position_y = 0


word_dict = {}
def button_function():
    word_dict[title] += 1

button_count = 0
def button_maker():
    global position_x, position_y, button_count, title
    button = tk.Button(window, text=counter_entry.get(), width=10, height=2, command = button_function, fg="red")
    button.place(x=position_x,y=position_y)
    position_x += 116
    button_count += 1
    if button_count % 6 == 0:
        position_y += 50
        position_x = 0
    title = counter_entry.get()
    word_dict[title] = 0
    counter_entry.delete(0,'end')




btnmaker = tk.Button(window, text='Click to create counter', width=17, height=2, command = button_maker, fg="red")
btnmaker.place(x=470,y=358)



btnreset = tk.Button(window, text='RESET', width=10, height=2, command = window.destroy, fg="red")
btnreset.place(x=520,y=500)

window.mainloop()