Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

python - How do I put 2 commands in 1 button

Im trying to put 2 commands to 1 button so I can destroy one of the commands which is only text, when a another button is pressed. But is says:

Play = Button(text="Play!", lambda: [f() for f in [entry, playernames]])                                                                           
                                                                       ^
SyntaxError: positional argument follows keyword argument.

heres the code, and it probably looks gross, but ive only been into coding for about 2 weeks.

from tkinter import *
from tkinter import filedialog, Text
import os



root = tk.Tk()
root.title("Number Guessing Game by Goasef")



#CANVAS
canvas = tk.Canvas(root, height=700, width=700, bg="#263D42", bd=0, highlightthickness=0)
canvas.pack(fill="both", expand=True)
canvas.create_text(350,70,fill="white",font=("Courier", 16, "italic"), text="THE NUMBER GUESSING GAME")
canvas.create_text(68,20,fill="white",font=("Arial", 12, "italic"), text="Game by Goasef")


def playernames():
      # enter players names
     canvas.create_text(83,300,fill="white",font=("Arial", 10, "italic"), text="Enter Player1 name: ")
     canvas.create_text(83,350,fill="white",font=("Arial", 10, "italic"), text="Enter Player2 name: ")

def entry(): #entry
    un_entry = Entry(root, font=("Arial"))
    un1_entry = Entry(root, font=("Arial"))

    un_entry.insert(0, "Player 1")
    un1_entry.insert(0, "player 2")
    
    def entry_clear(e):
        if un_entry.get() == 'Player 1' or un1_entry.get() == 'Player 2':
            un_entry.delete(0,END)
            un1_entry.delete(0,END)
      
    un_entry.bind("<Button-1>", entry_clear)
    un1_entry.bind("<Button-1>", entry_clear)
    
    def sg():
        un1_entry.destroy()
        un_entry.destroy()
        Ok.destroy()
        canvas.create_text(350,350,fill="white",font=("Helvetica", 16, "italic"), text="LET THE GAME BEGIN")


    un_window = canvas.create_window(250, 300, window=un_entry)
    un1_window = canvas.create_window(250, 350, window=un1_entry)
    
    #ok button
    Ok = Button(root, text="Ok!", font=("Arial", 8), width=5, command=sg)
    Ok_window = canvas.create_window(380, 350, window=Ok)

    

#Play Button
Play = Button(text="Play!", lambda: [f() for f in [entry, playernames]])
Play.configure(width=10, activebackground="green", relief=FLAT)
Play_window = canvas.create_window(50,100, window=Play)
Play.pack()

# Quit Button
Quit = Button(text="Quit", command=quit)
Quit.configure(width=10, activebackground="red", relief=FLAT)
Quit_window = canvas.create_window(10,68, window=Quit)
Quit.pack()


canvas.pack()
root.mainloop()```

question from:https://stackoverflow.com/questions/65643382/how-do-i-put-2-commands-in-1-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error you get is telling you exactly what is wrong - you can't have a positional argument after a keyword argument. You simply forgot the command= in front of the command.

Play = Button(text="Play!", command=lambda: [f() for f in [entry, playernames]]) 
                            ^^^^^^^^

Personally, I recommend against using lambda in this case. lambda just makes the code harder to understand the code and provides nothing in return. The code will be easier to understand and maintain if you use a proper function.

def play():
    entry()
    playernames()   
...
Play = Button(text="Play!", command=play)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...