How to set specific text to Entry in Python?

China☆狼群 提交于 2019-12-08 14:09:36

问题


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

This is the code:

    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.org/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()

回答1:


import sys
from tkinter import *

class C2F(Frame):

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.org/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")
    celsuisLabel = Label(labelframe, text="Celsius")
    fahrenheitLabel = Label(labelframe, text="Fahrenheit")
    celsuisLabel.grid(row=0, column=0)
    fahrenheitLabel.grid(row=1, column =0)

    self.celsius = StringVar()
    self.fahrenheit = StringVar()

    self.celsiusEntry=Entry(labelframe, bd=5,textvariable=self.celsius)
    self.celsiusEntry.grid(row=0, column=1)

    self.fahrenheitEntry=Entry(labelframe, bd=5,textvariable=self.fahrenheit)
    self.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 =self.celsius.get()
    if cgrade == '':
        self.fahrenheit.set('')
    else:
        cgrade=float(cgrade)
        fgrade=(cgrade-32)/1.8
        self.fahrenheit.set(str(fgrade))

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()`

Correction made:

1- You don't need to create global variables. Just create a reference to it using self. 2- To manipulate data in the Entry widget you need to first create a variable (either StringVar or IntVar or DoubleVar which are the tkinter equivalent for python variables). Once the variable has been set you need to sort of "map" it to the Entry widget. Doing this will enable you to get or set the entry widget content programmatically.

Cheers :)




回答2:


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.

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.

Here's an example of the latter:

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


来源:https://stackoverflow.com/questions/26082226/how-to-set-specific-text-to-entry-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!