如何在 Python 中将特定文本设置为 Entry?

编程入门 行业动态 更新时间:2024-10-24 12:20:55
本文介绍了如何在 Python 中将特定文本设置为 Entry?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在努力获取一个 python/tkinter 标签小部件来更新其内容.根据今天早些时候的一个线程,我按照有关如何组合小部件的说明进行操作.但是,在运行时,当我单击按钮 Calculeaza 时,标签小部件不会更改内容.据我所知,函数 Calculeaza() 是错误的.

I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, when I click button Calculeaza. As far as I can tell, function Calculeaza() is wrong.

def Calculeaza(self):
    cgrade =celsiusEntry.get()
    if cgrade == ' ':
        fahrenheitEntry.configure(text = ' ')
    else:
        cgrade=float(cgrade)
        fgrade=(cgrade-32)/1.8
        fahrenheitEntry.configure(text=str(fgrade))# is not function

这是代码:

    import sys
from Tkinter import *

class C2F(Frame):
    #celsiusEntry = Entry
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Convertor Celsius/Fahrenheit")
        self.pack(fill=BOTH, expand=1)
        # Meniul superior cu File>Exit si Help>About 
        menuBar= Menu(self.parent)
        self.parent.config(menu=menuBar)
        fileMenu= Menu(menuBar)
        fileMenu.add_command(label="Exit", command = self.onExit)
        menuBar.add_cascade(label="File", menu=fileMenu)
        # Adaugare butoane http://effbot/tkinterbook/grid.htm
        """
        Label(self.parent, text="First").grid(row=0, column =0)
        Label(self.parent, text="First").grid(row=1, column = 0)
        """
        labelframe = LabelFrame(self.parent, text="Celsius/Fahrenheit")
        labelframe.pack(fill="both", expand="yes")
        left = Label(labelframe, text="Celsius")
        left.grid(row=0, column=0)
        Label(labelframe, text="Fahrenheit").grid(row=1, column =0)

        global celsiusEntry
        celsiusEntry=Entry(labelframe, bd=5)
        celsiusEntry.grid(row=0, column=1)

        global fahrenheitEntry
        fahrenheitEntry=Entry(labelframe, bd=5, text="salut")
        fahrenheitEntry.grid(row=1, column=1)

        calcButon = Button(labelframe, text="Calculeaza", command=self.Calculeaza)
        calcButon.grid(row=1, column=2)

    def onExit(self):
        self.parent.quit()

    def Calculeaza(self):
        cgrade =celsiusEntry.get()
        if cgrade == ' ':
            fahrenheitEntry.configure(text = ' ')
        else:
            cgrade=float(cgrade)
            fgrade=(cgrade-32)/1.8
            fahrenheitEntry.config(text=str(fgrade))# is not function

def main():
    root= Tk()
    root.geometry("350x350+300+300")
    app= C2F(root)
    #Label(root, text="First").grid(row=0, column =0)
    root.mainloop()

if __name__ == "__main__":
    main()

推荐答案

条目小部件没有 set 方法.如果您正在阅读的某些文档表明确实如此,您可能需要联系作者以告诉他们他们是正确的.

Entry widgets don't have a set method. If there is some documentation you are reading that says it does, you might want to contact the author to tell them they are correct.

对于条目小部件,您有两种选择.一,如果你有一个 textvariable 关联,你可以在 textvariable 上调用 set .这将导致与 textvariable 关联的任何小部件被更新.其次,如果没有 textvariable,您可以使用 insertdelete 方法来替换小部件中的内容.

For an entry widget you have two choices. One, if you have a textvariable associated, you can call set on the textvariable. This will cause any widgets associated with the textvariable to be updated. Second, without a textvariable you can use the insert and delete methods to replace what is in the widget.

这是后者的一个例子:

fahrenheitEntry.delete(0, "end")
fahrenheitEntry.insert(0, cgrade)

这篇关于如何在 Python 中将特定文本设置为 Entry?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 05:28:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   中将   文本   如何在   Entry

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!