How to pass more arguments through tkinter bind

2024/10/5 14:52:21

How do I pass more arguments through tkinter's bind method?
for the example:

tk = Tk()
def moveShip(event,key):if event.keysym == 'Down' and player1.selectedCoord[1] != 9:if key == 'place':player1.selectedCoord[1] += 1elif event.keysym == 'Up' and player1.selectedCoord[1] != 0:if key == 'place':player1.selectedCoord[1] -= 1elif event.keysym == 'Left' and player1.selectedCoord[0] != 0:if key == 'place':player1.selectedCoord[0] -= 1elif event.keysym == 'Right' and player1.selectedCoord[0] != 9:if key == 'place':player1.selectedCoord[0] += 1tk.bind("<Key>", command=moveShip(event,'place'))

instead of

tk.bind("<Key>", moveship)

when I run the first one, it says the event is not defined

Answer

You can always wrap the callback function in a lambda.

tk.bind('<Key>', lambda event: moveShip(event, 'place'))

The other option is to use partial from functool to create a function with the default value of key already set.

from functools import partialmoveShip_place = partial(moveShip, key='place')tk.bind('<Key>', moveShip_place)
https://en.xdnf.cn/q/119663.html

Related Q&A

Python Class method definition : unexpected indent [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.I am getting started with Django and Python so naturally Im doing …

pandas python - round() not behaving correctly

Im rounding values in a dataframe to 1 decimal place. Here is the dfVren 2015 Hsten 2014 Vren 2014 Question 1) Maten r vllagad oc…

Converting dictionary into string

d={a:Apple,b:ball,c:cat}The above dictionary I have and I want my Output like the below-mentioned resultres="a=Apple,b=ball,c=cat"Is it possible in a pythonic way then please answer it I have…

Django Getting QuerySet.values() based on condition

Lets say I have a model Class Parent and a Class Child. And child has a field called status and a ForeignKey relationship to Parent.Lets say I retrieve one parent by calling filter (so as to have a Que…

Condition checking in python with user input [duplicate]

This question already has answers here:If...else statement issue with raw_input on Python(2 answers)Closed 8 years ago.I tried taking an input from keyboard. the checking that input with an if else sta…

Triangle of T in Python

EDIT ** I cant multiply strings by an integer. Its for a homework and those were the instructions **I need to do a triangle in python using for loops or while loops(mandatory). The final output should …

Finding prime project euler

Find the 10,001st prime number.I am trying to do Project Euler problems without using copying and pasting code I dont understand. I wrote code that finds whether a number is prime or not and am trying …

Splitting a python string

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a par…

file modifiaction and manipulation

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it …

Get quantitative value for color on two-color scale

I have run a chemical test that produces a color depending on how much of a given chemical is in the sample. It is green if there is no chemical, and yellow if there is a saturating amount of chemical.…