如何使用 tkinter Entry 小部件?

How to use the tkinter Entry widget?

我正在尝试在基于 tkinter 的 GUI 中插入一个 Entry 字段。 它有三个不同的 "pages" 作为 类 按钮等,但 Entry 字段不起作用。

我想将输入字段放在最后一页 (class PageTwo)。然后我只想单击其中一个复选按钮,如果输入的文本显示为打印,或者显示在 GUI 的另一个文本字段中,我就可以了。 更新:我将入口代码更改为 controller.wordde = Entry(self) 并制作了一个按钮 print(controller.wordde.get())

我得到的错误代码是:

Traceback (most recent call last):
  File "C:\x\g_4.py", line 147, in <module>
    app = SeaofBTCapp()
  File "C:\x\g_4.py", line 40, in __init__
    frame = F(container, self)
  File "C:\x\g_4.py", line 141, in __init__
    wordde.grid(row=3, column=1)
NameError: name 'wordde' is not defined

这是代码:

from tkinter import *
import tkinter as tk
import csv
import random
import sys

z = random.randint(1,50)
with open('vokabelnsemicolon.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')
    delist = []
    enlist = []
    for row in readCSV:
        eng = row[1]
        deu = row[0]

        delist.append(deu)
        enlist.append(eng)

LARGE_FONT= ("Verdana", 12)


class SeaofBTCapp(tk.Tk):


    def __init__(self, *args, **kwargs):


        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="German -> English",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = tk.Button(self, text="-> English -> German",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()



#German -> English
class PageOne(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="German -> English", font=LARGE_FONT)
        label.grid(row=0, column=0)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.grid(row=1, column=1)

        button2 = tk.Button(self, text="English -> German",
                            command=lambda: controller.show_frame(PageTwo))
        button2.grid(row=2, column=1)

        button3 = tk.Button(self, text="Check",
                            command=None)
        button3.grid(row=4, column=1)

        #text field for entry box
        Label(self, text='"'+enlist[z]+'"'+ ' in english is: ').grid(row=3, column=0)

        #entry field
        worden = Entry(self)
        worden.grid(row=3, column=1)


#English -> German
class PageTwo(tk.Frame):


    def checkybutton(self):
        print('checkbutton!')  

    def checkybutton2(self):
        print(controller.wordde.get())

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="English -> German", font=LARGE_FONT)
        label.grid(row=0, column=0)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.grid(row=1, column=1)

        button2 = tk.Button(self, text="German -> English",
                            command=lambda: controller.show_frame(PageOne))
        button2.grid(row=2, column=1)
        #text field for entry box
        Label(self, text='"'+delist[z]+'"'+ ' in german is: ').grid(row=3, column=0)


        button3 = tk.Button(self, text="checkbutton",
                            command=self.checkybutton)
        button3.grid(row=4, column=1)


        button4 = tk.Button(self, text="checkbutton 2",
                            command=self.checkybutton2)
        button4.grid(row=4, column=0)


        #entry field
        controller.wordde = Entry(self)

        wordde.grid(row=3, column=1)

        txt = Text(self, width=35, height=1, wrap=WORD)
        txt.grid(row=5, columnspan=2, sticky=W)    


app = SeaofBTCapp()
app.mainloop()

您必须将条目存储在某个地方,以便以后能够引用它。

对于您的代码,存储它的最佳位置似乎是在控制器中,因为它可以在所有页面上访问:

而不是 worden = Entry(self) 使用:

controller.worden = Entry(self)

这将在控制器对象中存储对您的入口实例的引用。稍后您可以使用 controller.worden.get() 检索值:

print(controller.worden.get())

编辑:澄清 - controllerSeaofBTCapp class 的实例,在此处创建:

app = SeaofBTCapp()

该实例在此处传递给其他 classes' __init__:

for F in (StartPage, PageOne, PageTwo):
    frame = F(container, self)  # self is the controller

现在,由于您需要在 __init__ 之外的其他方法中访问 controller,因此您还必须在那些 class 的实例中存储对控制器的引用]es.

StartPagePageOnePageTwo classes 中更改 __init__ 以执行此操作:

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    #.... rest of the method here ...

这意味着每个页面实例都有对作为属性存储的 controller 的引用。现在,在那些不是 __init__ 的 classes 的方法中,您可以使用 self.controller:

访问该对象
def checkybutton2(self):
    print(self.controller.wordde.get())