Не удается получить содержимое виджета ввода tkinter в Python 3 - PullRequest
0 голосов
/ 14 февраля 2020

Я начинаю с Python 3, и у меня возникают проблемы с получением значения из виджета tkinter. Когда я запускаю свой код, я получаю ошибку «имя edtGetMe не определено». Может кто-нибудь увидеть, где я иду не так?

from tkinter import *

# Define the class that forms my window.  
class Window(Frame):

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.master = master
    self.init_window()

# Init the class.
def init_window(self):
    # Place widgets on the window.
    self.pack(fill=BOTH, expand=1)

    # Quit button.
    btnCancel = Button (self, text="Cancel", command=self.cancel_out)
    btnCancel.place (x=10, y=120)

    # Action button.
    btnAction = Button (self, text="Set Text", command=self.action)
    btnAction.place (x=100, y=120)

    # The Editboxes.
    edtGetMe = Entry (self)
    edtSetMe = Entry (self)
    edtGetMe.place (y=10, x=10, width=380, height=100)
    edtSetMe.place (y=155, x=10, width=380, height=100)

def cancel_out(self):
    exit()

def action (self):
    # Get the entry from the GetMe box.
    self.usrText = edtGetMe.get()
    # Stuff it into the other box.
    edtSetMe.insert(self.usrText)

def main():
     root = Tk()

# Size of window.
root.geometry("400x300")

# Start Window Class. 
app = Window(root)
root.mainloop()

если name == " main ": main ()

1 Ответ

1 голос
/ 14 февраля 2020

Сначала я заменил edtGetMe на self.edtGetMe и то же самое для edtSetMe, поэтому эти переменные доступны из всех функций класса. Затем я добавил «вставить» в вашу строку self.edtSetMe.insert("insert", self.usrText). У вас также были проблемы с отступами.

Попробуйте это:

from tkinter import *

# Define the class that forms my window.  
class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    # Init the class.
    def init_window(self):
        # Place widgets on the window.
        self.pack(fill=BOTH, expand=1)

        # Quit button.
        btnCancel = Button (self, text="Cancel", command=self.cancel_out)
        btnCancel.place (x=10, y=120)

        # Action button.
        btnAction = Button (self, text="Set Text", command=self.action)
        btnAction.place (x=100, y=120)

        # The Editboxes.
        self.edtGetMe = Entry (self)
        self.edtSetMe = Entry (self)
        self.edtGetMe.place (y=10, x=10, width=380, height=100)
        self.edtSetMe.place (y=155, x=10, width=380, height=100)

    def cancel_out(self):
        exit()

    def action (self):
        # Get the entry from the GetMe box.
        self.usrText = self.edtGetMe.get()
        # Stuff it into the other box.
        self.edtSetMe.insert("insert", self.usrText)

def main():
    root = Tk()

    # Size of window.
    root.geometry("400x300")

    # Start Window Class. 
    app = Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()
...