简体   繁体   English

pyhton tkinter append 列表

[英]pyhton tkinter append list

I have written a bit of code, but my problem is it puts <tkinter.Entry object.!entry> every time I press the "add words" button.我写了一些代码,但我的问题是每次按下“添加单词”按钮时它都会放置<tkinter.Entry object.!entry> Any suggestions on how to fix this problem?有关如何解决此问题的任何建议?

import tkinter as tk

balls = tk.Tk()
CensoredList=[]

window1 = tk.Canvas(balls, width = 400, height = 300)
window1.pack()

title = tk.Label(balls, text="Bad Word Blocker")
title.config(font=("Bahnschrift", 14))
window1.create_window(200, 25, window=title)

subtitle = tk.Label(balls, text="Enter a word or phrase you 
would like blocked:")
subtitle.config(font=("Bahnschrift", 10))
window1.create_window(200, 100, window=subtitle)

entry = tk.Entry(balls) 
window1.create_window(200, 140, window=entry)

def fobblejibbits():
    CensoredList.append(entry)

def heebyjeeby():
    print(CensoredList)

pressme = tk.Button(balls, text ="add word to list", command = 
fobblejibbits)
pressme.pack()

pressme2 = tk.Button(balls, text ="are you done adding words", 
command = heebyjeeby)
pressme2.pack()

balls.mainloop()

You would need to add in a variable to associate as text with the entry field.您需要添加一个变量以作为文本与输入字段相关联。 Following is a code snippet that does that.以下是执行此操作的代码片段。

import tkinter as tk

balls = tk.Tk()
CensoredList=[]

name_var=tk.StringVar() # Add this

window1 = tk.Canvas(balls, width = 400, height = 300)
window1.pack()

title = tk.Label(balls, text="Bad Word Blocker")
title.config(font=("Bahnschrift", 14))
window1.create_window(200, 25, window=title)

subtitle = tk.Label(balls, text="Enter a word or phrase you would like blocked:")
subtitle.config(font=("Bahnschrift", 10))
window1.create_window(200, 100, window=subtitle)

entry = tk.Entry(balls, textvariable = name_var)    # Note the use of the string variable here
window1.create_window(200, 140, window=entry)

def fobblejibbits():
    name=name_var.get()         # Add this
    CensoredList.append(name)
    name_var.set("")            # Clear the field

def heebyjeeby():
    print(CensoredList)

pressme = tk.Button(balls, text ="add word to list", command = fobblejibbits)
pressme.pack()

pressme2 = tk.Button(balls, text ="are you done adding words", command = heebyjeeby)
pressme2.pack()

balls.mainloop()

The field added is the string variable "name_var".添加的字段是字符串变量“name_var”。

When I tested out the program, I pumped out a list to the terminal.当我测试程序时,我向终端输出了一个列表。

@Una:~/Python_Programs/Censored$ python3 Censored.py 
['Hello', 'Goodbye']

Give that a try.试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM