更新条目中的文本 (Tkinter) [英] Updating Text In Entry (Tkinter)

查看:24
本文介绍了更新条目中的文本 (Tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的一段代码通过表单从用户那里获取输入,然后返回乘以 2 的输入.我想要做的是,当用户输入一个数字(例如 5)并按下Enter"键时在键盘上或点击计算"按钮,他输入数字5"的地方也应该显示 10,除了下面的地方.通常,表单会保留输入的数字,但它下方的位置会更新并显示 10(假设您输入了 5)

The piece of code below takes input from user through a form and then returns the input as multiplied by 2. What I want to do is, when a user types a number (for example 5) and presses the "Enter" key on keyboard or clicks on "Calculate" button, the place where he entered the number "5" should also display 10, besides the place immediately below. Normally, the form keeps the number entered , but the place right below it gets updated and displays 10 (let us say you have entered 5)

如何更新表单位置?

(如果我的问题不清楚,请告诉我,以便我更好地解释自己.)

(Please let me know if my question is unclear, so I can better explain myself.)

from tkinter import *

def multiplier(*args):
    try:
        value = float(ment.get())
        result.set(value * 2)
    except ValueError:
        pass

mGui = Tk()
mGui.geometry("300x300+300+300")

ment = StringVar()
result = StringVar()

mbutton = Button (mGui, text = "Calculate", command = multiplier)
mbutton.pack()

mEntry = Entry(mGui, textvariable = ment, text="bebe")
mEntry.pack()

mresult = Label(mGui, textvariable = result)
mresult.pack()

推荐答案

您可以使用 Entrydeleteinsert 方法.

You can use Entry's delete and insert methods.

from tkinter import *

def multiplier(*args):
    try:
        value = float(ment.get())
        res = value *2
        result.set(res)
        mEntry.delete(0, END) #deletes the current value
        mEntry.insert(0, res) #inserts new value assigned by 2nd parameter

    except ValueError:
        pass

mGui = Tk()
mGui.geometry("300x300+300+300")

ment = StringVar()
result = StringVar()

mbutton = Button (mGui, text = "Calculate", command = multiplier)
mbutton.pack()

mEntry = Entry(mGui, textvariable = ment, text="bebe")
mEntry.pack()

mresult = Label(mGui, textvariable = result)
mresult.pack()

这篇关于更新条目中的文本 (Tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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