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

时间:2019-11-15 20:26:06

标签: python function tkinter tkinter-entry

正在研究一个项目,在该项目中,我使用Tkinter来创建一个GUI,该GUI在下拉菜单中提供软件列表,并且在选择特定软件时,它将带您到一个单独的窗口,在该窗口中将显示用户名输入,它们将被添加到数据库中。使用到目前为止的代码,我可以将第二个窗口上的“提交”按钮链接到一个功能,该功能可以打印确认消息作为测试,以确保该按钮可以正常工作。我现在的问题是尝试从输入字段中获取输入并将输入链接到“提交”按钮,但是我似乎找不到解决方法。我想知道是否可以就此进行一些建议。是否需要使用类才能使其正常工作?还是可以坚持使用功能并使代码保持相对简单?

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

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()

1 个答案:

答案 0 :(得分:0)

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

在您的情况下:

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")

希望有帮助