How to pass arguments to a Button command in Tkinter?

前端 未结 18 2827
夕颜
夕颜 2020-11-21 07:42

Suppose I have the following Button made with Tkinter in Python:

import Tkinter as Tk
win = Tk.Toplevel()
frame = Tk.Frame(master=win).grid(row=         


        
18条回答
  •  星月不相逢
    2020-11-21 08:16

    Use a lambda to pass the entry data to the command function if you have more actions to carry out, like this (I've tried to make it generic, so just adapt):

    event1 = Entry(master)
    button1 = Button(master, text="OK", command=lambda: test_event(event1.get()))
    
    def test_event(event_text):
        if not event_text:
            print("Nothing entered")
        else:
            print(str(event_text))
            #  do stuff
    

    This will pass the information in the event to the button function. There may be more Pythonesque ways of writing this, but it works for me.

提交回复
热议问题