python tkinter if no button pressed run function - python

I am currently working on a roulette game in tkinter. The problem is that I want the roulette automatically to run after a ceirtan time (20sec). But right now it will only run if the player inputs something inform of pressing a button and entering an amount of money. But I want the roulette to run every 20 sec even if the player didnt bet anything(so no button got pressed). But I cant seem to find out how.
pseudocode:
if no button got pressed after 20 sec:
run roulette #(roll roulette without player input and without his money)

Not having any code makes answering this a bit harder. I believe what you're looking for is after.
This could run after a time period to check if the user has clicked to play. You should have a variable monitoring the user's clicks and reseting once the game's done.
some real code:
clicked = False
def force_play():
if not clicked:
play()
after(1000 * 20, force_play)

Related

Is there a way to let Python differentiate clicks sent by the program and clicks sent by the user?

for some context, I'm trying to make a python program that left-clicks for you when the left mouse button is held down.
This is how it works: while True:
keystate = win32api.GetAsyncKeyState(0x02) or win32api.GetAsyncKeyState(0x01)
if left_clicker == 'true':
if keystate == win32api.GetAsyncKeyState(0x01):
ac()
if keystate == win32api.GetAsyncKeyState(0x02):
if right_clicker == 'true':
rightac()
The ac() loop clicks and then waits for a delay, and then repeats the loop. However, when I run the code, I hold down the mouse button, it clicks once, and then stops. The reason is the program reads the left click sent by Python and sees that the left mouse button has been released (which must happen upon click or the loop will run indefinitely regardless of how the user interacts with the mouse). So, is there any way I can let Python differentiate clicks sent by the program and directly by the user so it won't stop itself? The only workaround I have found is using a different button to trigger the loop, but that's not really what I'm trying to do. Please ask for clarification if needed
I have tried using Pynput and PyAutoGUI as the method for clicking the mouse, both had the same issue. I don't know of any other libraries that can click the mouse and I'm out of ideas on how to solve this.

How to disable a certain key from the keyboard until a function is called upon at least once?

I have created a game which has a pause feature. Once the player presses the escape key whilst a game is playing, the menu enables, once escaped is pressed again it disables the menu and resumes the game. But when i open my program and press escape without launching a game first the game crashes causes theirs no game to resume. How could i disable the escape key until at least one game has started.
I am using the Pygame-menu module by ppizarror to create my gui, iv'e tried looking up my question but no one has a clear answer. Below is the main parts of code from two different classes that handles the pause feature.
Class EntertheGauntlet:
# a function used throughout the menus that disables the previous menu for
the new one to be displayed
def resume_feature():
# disables menu
self.main_menu.disable()
# resumes the game paused in the background by calling upon it
from the main gameloop
self.game.main()
Class GUI:
# a function used throughout the game that initiates when the new
game button is pressed
def start_new():
# disable previous menu
self.main_menu.disable()
# Start new game by calling variable from EscapeTheGauntlet.py
self.game = EscapeTheGauntlet(self.window, clock, self.sfx)
# sets this new updated screen to the new main window
self.game.main()
The only solution i can see is disabling the escape key until the start new game feature is called upon at least once. Could you please show me how i would do that.
What you're suggesting sounds unnecessarily complex. You cannot mess with the system drivers of the keyboard, or with the key-to-character mapping, because the user might be working in a different window with a different program where the escape key is needed.
Can't you re-write your code that handles the pause function to check whether there is a paused game? Initialize some variable with False when the application starts, and set it to True when the function you expect to be called is called.

Psychopy on Surface Pro 6: Can touchscreen be used to press AND hold a button similar to a mouse click?

SPECS:
Using PsychoPy v1.90.3
Window 10 Pro 6 running Windows 10
BACKGROUND:
I am programming a touchscreen task to use with children (reference image linked below). In the task, children need to press and hold the red 'home' button for 1.5 seconds at the bottom of the screen to advance through trials(i.e., opening the windows at the top of the screen to reveal boxes they can open). I also collect the time of the mouse button being pressed and released (used to calculate response time).
I originally programmed this task on my desktop using a mouse, so pressing and holding the home button was no problem using this code:
mouse = event.Mouse(visible=True)
while not homePressed:
if mouse.isPressedIn(home) and home.contains(mouse):
core.wait(1.5, hogCPUperiod=1.5) ## when home button is pressed, wait for 1.5s
if mouse.isPressedIn(home) and home.contains(mouse): ## check if home button is still pressed
homePressed=True
When I tried to run the task on the Surface Pro I ran into a problem with the touchscreen not registering a 'press and hold'. I've learned that the touchscreen doesn't register mouse clicks unless the screen has been pressed AND released because a press and hold could be (1) a right click or (2) a swipe. I've tried disabling the 'press and hold' registering as a right-click option on the Surface Pro but this has not solved my issue.
QUESTIONS:
Is there a way to get the Surface Pro or PsychoPy to register a press and hold on the touchscreen the same way it does using a mouse so children press the 'home' button down to continue the trials?
If yes, can I get PsychoPy to output the 'press' (when the screen is touched) and 'release' (when the touch is no longer present) output the same way it does for a mouse click?
If this cannot be accomplished with the PsychoPy library, are there possible python solutions outside of PsychoPy I could try?
SOLUTIONS TRIED:
using only home.contains(mouse) solution found here
resetting the mouse location solution found here
fixing the 'double tap' issue solution found here
Disabling the Surface Pro's right-click function for touch
task set up image
You could try and write a loop that checks the hold time yourself. For example, in a gaze contingent study we have loops to check how long someone has been looking at something, similar to your "hold" variable.
tCueOn=expClock.getTime()
while True:
curtime=expClock.getTime()-tCueOn
eventType=eyelink.getNextData()
sample=eyelink.getNewestSample()
<<... a bunch of sample processing cut out ...>>
if curtime>=cueTime:
break
In your case you detect the press, get the time, and enter the loop where you repeatedly check the press status is still true and that the time is less than 1.5. When it exceeds 1.5 you break the loop, or if they let up for less than 1.5 you return to whereever you need to in your use case. You might find it convenient to bundle this logic in a function that you could just call whenever "press home" is true.
Hope this helps, cheers.

How to focus on a window with pygame?

I made a Python game using Pygame. I try to make it so when it starts, it loads the shell, and entering a raw input would display the pygame window and start the game so that I can make the game playable without having to close the shell. It works, however, the window starts minimized. The game is a simple "dodge the object" and has no pause what so ever. The game still runs in the background, possibly having the player hit multiple projectiles before the user realizes it. Is there a way to focus on the window?
For anyone in the future who stumbles across this question:
As of 2022, pygame v2.1.2 has an experimental module ._sdl2 where: Window.focus() is used to make a window to be moved at the top and set focus. Windows.focus() supports optional input_only bool parameter for whenever the window should be moved at the top (False), or just collect input (True).
As I understood you, you don't want the game to start, before the window is in fullscreen-mode, right?
When I try you attempt (starting through rawinput), my display is fullscreen from the start. Have you set everything correctly?
I suggest that you stop the game until there is an actual key-input (whatever controls you have set). Like this the player has the time to arrange everything to his liking before starting the game. Because, even if you figure out how to analyse the focus-issue: When the game starts, the window HAS focus, therefore this approach wouldn't work anyway.

Timer in Python

I am writing a python app using Tkinter for buttons and graphics and having trouble getting a timer working, what I need is a sample app that has three buttons and a label.
[start timer] [stop timer] [quit]
When I press the start button a function allows the label to count up from zero every 5 seconds, the stop button stops the timer and the quit button quits the app.
I need to be able to press stop timer and quit at any time, and the time.sleep(5) function locks everything up so I can't use that.
currently i'm using threading.timer(5,do_count_function) and getting nowhere !
I'm a vb.net programmer, so python is a bit new to me, but hey, i'm trying.
Check the .after method of your Tk() object. This allows you to use Tk's timer to fire events within the gui's own loop by giving it a length of time and a callback method.

Categories

Resources