0

我正在使用 Tkinter 和 ttk 构建我的第一个 UI,并且努力链接一个按钮单击事件以收集两个小部件值。我想创建一个“设置”菜单作为子框架,并具有修改和读取两个文本小部件的当前值的功能。

所以,我想:

  1. 能够从小部件中添加或删除文本。
  2. 使用按钮单击事件,收集当前小部件值并在单独的函数中使用它们

使用我目前的方法,我不确定如何读取两个小部件的值(以防用户更改它们),因为“command = lambda”只允许我发送一个参数。

我正在考虑使用“GetValues”按钮单击事件调用函数,但不确定如何从函数访问文本小部件。

这是我的代码

    from tkinter import *
from tkinter import ttk

# Print  values from both Text Widgets
def getValues():

    print(TextAreaRegion.get("1.0","end-1c"))
    print(TextAreaService.get("1.0", "end-1c"))

#Create a child window for Settings
def openSettingsWindow():
    settingsWindow = Toplevel(root)
    settingsWindow.title("Settings")
    settingsWindow.geometry('640x480+100+100')
    settingsWindow.resizable(False, False)
    Label(settingsWindow, text="Settings").pack()

    frameLeft = ttk.LabelFrame(settingsWindow)
    TextAreaRegion = Text(frameLeft, width=60, height=2, wrap='word')


    frameLeft.pack()
    frameLeft.config(height=400, width=300, relief=RIDGE, padding=(30, 15), text="Settings")

    ttk.Label(frameLeft, text="Regions:").grid(row=0, column=0, sticky='w', padx=5, pady=10)
    ttk.Label(frameLeft, text="Tenants:").grid(row=1, column=0, sticky='w', padx=5, pady=10)

    TextAreaRegion.grid(row=0, column=1, sticky='w')
    TextAreaRegion.insert('1.0 + 2 lines', "RegionOne, RegionTwo")

    TextAreaService = Text(frameLeft, width = 60, height = 2)
    TextAreaService.grid(row=1, column=1, sticky='w')
    TextAreaService.insert('1.0 + 2 lines', "ServiceOne, ServiceTwo")

    #Button
    buttonSave = ttk.Button(frameLeft, text="Get values", command=getValues)
    buttonSave.grid(row=3, column=0, sticky='w', padx=5)

root = Tk()

#Menu bar
root.option_add('*tearOff', False)
menubar = Menu(root)
root.config(menu = menubar)
file = Menu(menubar)
menubar.add_cascade(menu = file, label = 'File')

#Add menu elements
file.add_command(label = 'Settings', command = openSettingsWindow )

root.mainloop( )

如果我使用 lambda,我可以获得其中一个小部件的值,但是,我不能为 lambda 使用两个参数:

buttonSave = ttk.Button(frameLeft, text="Get values", command=lambda:print(TextAreaService.get("1.0", "end-1c")))
4

1 回答 1

0

这是带有多个参数的 lambda 函数的工作代码。

from tkinter import *
from tkinter import ttk

# Print  values from both Text Widgets
def getValues(region_box, service_box):
    print(region_box.get("1.0","end-1c"))
    print(service_box.get("1.0","end-1c"))


#Create a child window for Settings
def openSettingsWindow():
    settingsWindow = Toplevel(root)
    settingsWindow.title("Settings")
    settingsWindow.geometry('640x480+100+100')
    settingsWindow.resizable(False, False)
    Label(settingsWindow, text="Settings").pack()

    frameLeft = ttk.LabelFrame(settingsWindow)
    TextAreaRegion = Text(frameLeft, width=60, height=2, wrap='word')


    frameLeft.pack()
    frameLeft.config(height=400, width=300, relief=RIDGE, padding=(30, 15), text="Settings")

    ttk.Label(frameLeft, text="Regions:").grid(row=0, column=0, sticky='w', padx=5, pady=10)
    ttk.Label(frameLeft, text="Tenants:").grid(row=1, column=0, sticky='w', padx=5, pady=10)

    TextAreaRegion.grid(row=0, column=1, sticky='w')
    TextAreaRegion.insert('1.0 + 2 lines', "RegionOne, RegionTwo")

    TextAreaService = Text(frameLeft, width = 60, height = 2)
    TextAreaService.grid(row=1, column=1, sticky='w')
    TextAreaService.insert('1.0 + 2 lines', "ServiceOne, ServiceTwo")

    #Button - Note you can pass multiple arguments in a lambda function like here
    buttonSave = ttk.Button(frameLeft, text="Get values", command=lambda region_box=TextAreaRegion, service_box=TextAreaService: getValues(region_box, service_box))
    buttonSave.grid(row=3, column=0, sticky='w', padx=5)



root = Tk()

#Menu bar
root.option_add('*tearOff', False)
menubar = Menu(root)
root.config(menu = menubar)
file = Menu(menubar)
menubar.add_cascade(menu = file, label = 'File')

#Add menu elements
file.add_command(label = 'Settings', command = openSettingsWindow )

root.mainloop( )
于 2020-07-15T07:10:45.647 回答