繁体   English   中英

如何使用tkinter Entry小部件?

[英]How to use the tkinter Entry widget?

我正在尝试在基于tkinter的GUI中插入Entry字段。 它具有三个不同的“页面”作为带有按钮等的类,但是“ 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())

编辑:要澄清- controller是在此处创建的SeaofBTCapp类的实例:

app = SeaofBTCapp()

该实例将传递给其他类的__init__

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

现在,由于您将需要使用不是__init__其他方法访问controller ,因此还必须在这些类的实例中存储对控制器的引用。

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

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

这意味着每个页面距离都将对controller的引用存储为属性。 现在,在那些不是__init__类的方法中,您可以使用self.controller访问该对象:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM