Tkinter GUI 程序问题。 Entry.get() 问题

Tkinter GUI program issue. Entry.get() problem

Working on a project in which I use Tkinter in order to create a GUI that gives a list of software in a drop-down and when a particular software is chosen, it takes you to a separate window将在其中输入用户名并将其添加到数据库中。使用我目前的代码,我能够 link 第二个 window 上的 "submit" 按钮到一个函数,该函数打印确认消息作为测试以确保按钮有效。我现在的问题是尝试从输入字段获取输入,并从 link 获取 "Submit" 按钮的输入,但我似乎找不到这样做的方法。我想知道我是否可以得到一些关于如何去做的建议。是否需要使用 类 才能使其正常工作?或者我可以坚持使用函数并使代码相对简单吗?

我在下面添加了我的程序的代码。

import tkinter as tk
from tkinter import *
from tkinter import ttk

root = tk.Tk()                                  # Main window
root.title("Software Licences")
root.geometry("300x300")

frame = ttk.Frame(root, padding="50 0 50 50")
frame.pack(fill=tk.BOTH, expand=True)

tkvar = StringVar()

choices = ['Imagenow',                          # Dropdown menu with software options
           'FileMakerPro',
           'Acrobat',
           'Office',
           'Lotus Notes']

tkvar.set('Acrobat')                            # Shows default dropdown menu option when program is opened

popupMenu = OptionMenu(frame, tkvar, *sorted(choices))
popupLabel = ttk.Label(frame, text="Choose Software")

popupLabel.pack()
popupMenu.pack()


def software_pages():                           # In this function is the 2nd window with for each individual software
    top = Toplevel()
    top.title("Software Licences")
    top.geometry("300x300")
    myLabel = Label(top, text=tkvar.get()).pack()
    employee_entrylbl = Label(top, text="Employee name").pack()
    employee_entry = Entry(top, width=25, textvariable=tk.StringVar) # Entry field for adding user's name
    employee_entry.pack() # Entry field is displayed

    if tkvar.get() == "Acrobat":                # for each if statement, button command is link to the functions
        # defined below
        button = ttk.Button(top, text="Submit", command=add_to_acrobat).pack()
    elif tkvar.get() == "Imagenow":
        button = ttk.Button(top, text="Submit", command=add_to_imagenow).pack()
    elif tkvar.get() == "FileMakerPro":
        button = ttk.Button(top, text="Submit", command=add_to_filemakerpro).pack()
    elif tkvar.get() == "Office":
        button = ttk.Button(top, text="Submit", command=add_to_office).pack()
    else:
        button = ttk.Button(top, text="Submit", command=add_to_lotusnotes).pack()

    exit_button = ttk.Button(top, text="Exit", command=top.destroy).pack() # Exit button for second window


add_emp_button = ttk.Button(frame, text="Next", command=software_pages) # "Next" button in the main window takes the
# user to the second window
add_emp_button.pack()

# Functions below are linked to the button commands of each software in the second window function defined earlier.
# They print out specified messages that confirm the user had been added


def add_to_acrobat():
    return print("User added to Acrobat")


def add_to_lotusnotes():
    print("User added to IBM")


def add_to_imagenow():
    print("User added to imagenow")


def add_to_office():
    print("User added to 365")


def add_to_filemakerpro():
    print("User added to FMP")


def click_button():                         # Function for Exit button for main window
    root.destroy()


exit_button = ttk.Button(frame, text="Exit", command=click_button)  # Exit button for main window
exit_button.pack()

root.mainloop()

您可以使用 functools 模块中的 partial 将参数传递给 tkinter.command 的命令。

你的情况:

button = ttk.Button(top, text="Submit", command=partial(add_to_acrobat, employee_entry)).pack()

在上面的行中,我将 employee_entry(包含您想要的文本)发送到 add_to_acrobat 函数

和 add_acrobat 函数应该如下所示:

def add_to_acrobat(e):
    print(e.get())
    return print("User added to Acrobat")

希望对您有所帮助