I'm working on a little game with Tkinter, but I'm facing a issue and I dont know how to fix it
The issue is that sometime when I press a button in the game (like the right arrow to move right for example), the function corresponding to the button is working well, but when I press spacebar just after, it calls the function again which is something I dont want.
So I'm wondering if there is any way to completly disable the space bar for tkinter ?
I've already tried 2 things:
Bind the spacebar to another function which is doing nothing, but when I press the spacebar it is still repeating the last button pressed
Unbind the space bar at the start of the code like this:
import tkinter as tk
game = tk.Tk()
game.unbind('<space>')
If you want, here is the full code of the game:
https://github.com/Nirs123/World_Of_Boats
Appreciate every feedback :)
Here is an answer of what I said in my comment.
You can use game.unbind_class() to unbind a specific event for a specific type of widget. To unbind the spacebar from a button, you can use game.unbind_class("Button", "<Key-space>"). You can call it right after you create game, and all the buttons in the whole window will no longer be able to be pressed with the spacebar.
Related
Picture of my GUI for clarity
I made a tool that plays audio from youtube. I want it to pause when the spacebar is pressed. I do this by this piece of code:
win.bind('<space>',lambda event:funcPP(player, btnPP))
win is my window, funcPP is the play/pause function.
My problem is that I have also an Entry in my window (for searching videos) and of course it is impractical if the video pauses everytime when I search a new video and press Spacebar. And the real problem is that, once I have clicked the Entry field, the focus stays there. It doesn't go away after clicking somewhere else! This sabotages my Spacebar shortcut.
Any tips how I can solve this?
I found the solution.
With:
win.bind_all("<Button-1>", lambda event: event.widget.focus_set())
It is possible to put the focus whereever one clicks.
then:
win.bind('<space>',lambda event:funcPPTRANSFER(player))
To bind spacebar to the whole window
then:
def funcPPTRANSFER(player):
if str(win.focus_get()) != str(".!entry"):
funcPP(player)
Have the function check whether the spacebar was sent from the entry or from somwhere else in the window
EDIT: Another thing I found now:
https://stackoverflow.com/a/56519799/18664063
if isinstance(event.widget,tk.Tk): #check if event widget is Tk root window
very nice function that might help as well in this project.
I would like to improve my program, I just do not know how.
I want that an event starts, when the users mouse is over a tkinter's widget.
This...
button.bind("<Motion>", lambda eff: callback())
... is working, but the users needs to move the mouse for call the function.
Is there a way to start, for example, every x seconds the function while the mouse is over the widget?
I need it to display an integer that changes constantly as text of a button or a label while the mouse is over the button or label.
Do you have any Ideas?
Thanks for your attention,
Lukas
You can bind a function to <Enter> to trigger a function when the mouse enters a widget, and you can bind to <Leave> to trigger when it leaves.
The <Enter> binding can run a function that starts updating the label periodically using after. The <Leave> binding can set a flag that the update function checks in order to know to stop updating.
There are many questions and answers on this site about using after to do something periodically. For example, How to use the after method to make a callback run periodically?
Python 3
Tkinter
Hey, I'm looking for something that don't allow the user to leave a window if a variable is true/false.
basicly, somethings that keeps the mouse inside the tkinter window
Not directly, no. However you could monitor the mouse position with the <Motion> event and correct it every time it goes outside. How to correct it would depend on your OS; look into pyautogui for a crossplatform solution.
You can't force the mouse to stay in the window, though you can "grab" all of the mouse events, preventing the user from clicking anywhere but on your app. This is very dangerous as you can lock up your computer if your code has a bug in it.
See How can I make area outside toplevel unclickable?
Is it possible in Tkinter to avoid the event grab which occures when you press a mouse button over a widget and keep it pressed while you move the mouse?
I want to register the mouse button and then track all widgets the user enters while he moves his mouse with the button pressed. When the user releases the mouse button the application executes the same action for all tracked widgets.
The following code should explain what I want to do.
# Set a tracking flag
widget.bind('<Button>', start_tracking)
# Add the entered widget to the tracked widgets, if the tracking flag is set
widget.bind('<Enter>', add_to_tracked_widgets)
# Execute an action for every tracked widget; unset the flag
widget.bind('<ButtonRelease>', end_tracking)
I took a look at the grab_current and grab_status methods, but they always returned None.
Python version is 3.4.1.
This is probably the most complicated way to do this, but okay.
One thing that makes this more complicated is Tkinter itself, because event.widget still refers to the widget that was clicked on initally. A different event we can use is Motion which is activated when the mouse moves inside a widget.
tk.bind("<Motion>", add_tracked)
I think you can implement the list and state variables yourself, so we come to the add_tracked method (I just renamed it, it's your add_to_tracked_widgets):
def add_tracked(event):
if tracking:
# Get coordinated of the event and use the master window method to determine
# wich widget lays inside these.
widget = tk.winfo_containing(event.x_root, event.y_root)
# Since 'Motion' creates many events repeatedly, you have to convert this
# list into a set to remove duplicates.
widgets.append(widget)
In a pygame application window, the minimize, resize and close buttons are present. Is there a way to disable the close(X) button?
I don't think there is, because some window managers don't give you the ability to remove the close button. But you can write an event handler such that the close button does whatever you want, including nothing.
Why do you want to prevent the user from closing? If it's just a matter that you would rather provide an in-game "quit" button that confirms and/or saves before quitting, you can perform the same task when the user hits the close button.
Just for the record, another option would be to pass the following argument to the set_mode() method call:
pygame.display.set_mode(..., flags = pygame.NOFRAME)
This however makes the whole frame go away, including the top strip to move the window around and the other buttons, such as minimize, so it's rather overkill for just getting rid of the X button.