从tkinter条目小部件捕获文本

时间:2018-01-15 04:17:27

标签: python tkinter tkinter-entry

我正在尝试运行以下脚本,以便使用tkinter将用户输入的文本捕获到表单中。

from tkinter import *

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

    # create init window
    def init_window(self):
        self.master.title("Example Title")
        self.pack(fill = BOTH, expand = 1)

        #submit button
        submitButton = Button(self, text = "submit", command = self.showEntry).grid(row = 6, column = 1, sticky = W, pady = 5)

        # entry widgets
        Label(self, text = "Firstname").grid(row = 0)    
        Label(self, text = "Surname").grid(row = 1)
        Label(self, text = "Age").grid(row = 2)    
        Label(self, text = "Gender").grid(row = 3)

        e1 = Entry(self)
        e2 = Entry(self)
        e3 = Entry(self)
        e4 = Entry(self)

        e1.grid(row=0, column=1)
        e2.grid(row=1, column=1)
        e3.grid(row=2, column=1)
        e4.grid(row=3, column=1)

    # show entries
    def showEntry(self):
        txt = e1.get()
        print("Firstname is %s" % txt)
        root.destroy()  


root = Tk()
root.geometry("400x300")
app = Window(root)

app.mainloop()

执行此操作时,会出现以下NameError:

  

NameError:未定义名称“e1”

我对tkinter很新,所以对如何获取用户输入的文本以及为什么会出现此错误的任何帮助都将非常感激。

由于

1 个答案:

答案 0 :(得分:1)

不是WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'appadmin' HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default' HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode ?: (urls.W005) URL namespace 'dev_app' isn't unique. You may not be able to reverse all URLs in this namespace Operations to perform: Apply all migrations: admin, auth, contenttypes, dev_app, sessions Running migrations: Applying dev_app.0016_auto_20180112_0630...Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line utility.execute() File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/usr/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle fake_initial=fake_initial, File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration state = migration.apply(state, schema_editor) File "/usr/lib/python2.7/site-packages/django/db/migrations/migration.py", line 129, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/usr/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 86, in database_forwards field, File "/usr/lib/python2.7/site-packages/django/db/backends/mysql/schema.py", line 48, in add_field super(DatabaseSchemaEditor, self).add_field(model, field) File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 414, in add_field definition, params = self.column_sql(model, field, include_default=True) File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 154, in column_sql default_value = self.effective_default(field) File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 228, in effective_default default = field.get_db_prep_save(default, self.connection) File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save prepared=False) File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2277, in get_db_prep_value value = self.get_prep_value(value) File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2272, in get_prep_value return self.to_python(value) File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2259, in to_python params={'value': value}, django.core.exceptions.ValidationError: [u"'null' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."] 而是tkinter问题。

您必须使用OOP来创建存在于类

中所有方法中的变量
self.

以后

self.e1 = Entry()

目前self.e1.get() 是局部变量,仅存在于e1