使用按钮tkinter设置条目的文本 [英] set the text of an entry using a button tkinter

查看:113
本文介绍了使用按钮tkinter设置条目的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的回覆。
我正在用Tkinter弄湿我的脚。
我正在使用Tkinter在GUI中使用按钮设置条目的文本。
这个GUI是帮助我分类数千个单词。分为五个类别,每个类别都有一个按钮。我希望使用一个按钮会显着加快我的速度,我想每次重复检查一次,否则我只需使用按钮,并让GUI处理当前的单词并带上下一个单词。
由于某些原因,命令按钮的行为不像我想要的那样。
这是一个例子

  def cWin():
win = Tk()
v = StringVar()
def setText(word):
v.set(word)


a = Button(win,text =plant,command = setText (plant)
a.pack()
b = Button(win,text =animal,command = setText(animal))
b.pack()
c = Entry(win,textvariable = v)
c.pack()
win.mainloop()

到目前为止,当我能够编译点击什么也没有。



我感谢所有的帮助。

解决方案

您可能想使用 insert 方法。



此脚本将文本插入 Entry 中,插入的文本可以在Button的命令参数中更改。 p>

 从tkinter import * 

def set_text(text):
e.delete(0, END)
e.insert(0,text)
return

win = Tk()

e = Entry(win,width = 10)
e.pack()

b1 = Button(win,text =animal,command = lambda:set_text(animal))
b1.pack()

b2 = Button(win,text =plant command = lambda:set_text(plant))
b2.pack()

win.mainloop()


Thanks for reply. I am getting my feet wet with Tkinter. I am trying to set the text of an entry using a button in a GUI using Tkinter. This GUI is to help me classify thousands of words. into five categories, each of the categories has a button. I was hoping that using a button would significantly speed me up and I want to double check the words every time otherwise i would just use the button and have the GUI process the current word and bring the next word. The command buttons for some reason are not behaving like i want them to. This is an example

def cWin():
  win = Tk()
 v=StringVar()
 def setText(word):
     v.set(word)


a = Button(win, text="plant", command=setText("plant")
a.pack()
b = Button(win, text="animal",command=setText("animal"))
b.pack()
c = Entry(win, textvariable=v)
c.pack()
win.mainloop()

so far when i am able to compile the click does nothing.

I appreciate all help.

解决方案

You might want to use insert method.

This script inserts a text into Entry. The inserted text can be changed in command parameter of the Button.

from tkinter import *

def set_text(text):
    e.delete(0,END)
    e.insert(0,text)
    return

win = Tk()

e = Entry(win,width=10)
e.pack()

b1 = Button(win,text="animal",command=lambda:set_text("animal"))
b1.pack()

b2 = Button(win,text="plant",command=lambda:set_text("plant"))
b2.pack()

win.mainloop()

这篇关于使用按钮tkinter设置条目的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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