So, I'm currently planning a program using the Easygui module,
The problem I am facing is, when I press the cancel button on the GUI, it just continues, so to get past this, I want to add something to my program like:
if Easygui_Variable is None:
quit()
However, the problem I face is, I am going to have multiple easygui boxes which allow the user to press cancel, and I don't want to be using
if Easygui_Variable is None:
quit()
every time, so is there anyway I can do the above for multiple variables at a time, to make sure my program is most efficient.
Related
I have a program that detects when a certain key is pressed, and it will press another key. I'm not the greatest at python, but I've tried everything I could to get this to work. My game will end after the hello world statement I've added as a test. Could somebody please tell me a way to fix this? (Note: I want the code to always stay running, waiting for user input)
https://i.stack.imgur.com/QeJ3r.png
I'm trying to add a tkinter GUI to a simple text-based game made in python but I'm struggling with the game loop, which either runs infinitely or not at all. I would like the game to ask the player what they want to do, then do something based on that, then return to asking the player what they want. The problem I'm having is instead of calling the function once, it calls it repeatedly.
while playing is True:
getAreaDesc()
requestPlayerMove()
if action == 1:
explore()
elif action == 2:
travel()
elif action == 3:
rest()
window.update_idletasks()
window.update()
I'm fairly sure I know what is causing the problem. As I understand it, this is because the value for action is determined by a tkinter button (with a command setting the value for action) so as soon as a button is pressed, it calls the relevant function. Then it checks again but as the action is still the same, it calls the function again. And so on and it doesn't stop until a different button is pressed and a different. I think it's like a lightswitch in the sense that once it's on it stays on, even though the switch is only flicked once.
However, even if I am right about why it calls the functions continuously, I can't think of a way to prevent that occuring which doesn't simply stop the loop.
I've tried resetting action as part of the functions, but that causes it to do nothing beyond ask for player choice. Similarly, trying to nest the if/elif parts comes to the same result, and using after() changes nothing but the speed at which it loops relentlessly. And those were my good ideas. I tried putting input() back in to wait for the player and that went about as well as you could imagine.
I'm sure it's something daft I'm missing, hopefully not offensively stupid, but I'm at a loss and any help would be very appreciated. Thanks for your time reading this, if your patience lasted this far.
I'm learning how to program in Python and use the PIL (Pillow) in order to create small bots which can recognize changes in the screen in order to execute repetitive commands (keyboard presses with pynput module).
I did a small GUI with Tkinter in order to get user information regarding the parameters which the user is going to use (Entry widgets) at their use of the bot and I also need a few buttons for enabling/disabling certain parts of the code (i'm using check buttons and using boolean logic in order to enable/disable) and also for enabling the main function of the bot (which is to press a button once it recognizes a certain change at the screen).
What I am currently experiencing is that the once I run my program, it opens up my GUI and all that a user would see but it doesn't execute my main function, it only executes the main function AFTER I close the GUI.
I'd love to post my code but it exceeds 220 lines and it would probably become even more confusing.
So, long story short, is it a kind of error which happens a lot when inexperienced programmers try to create softwares with tkinter? If yes, what causes it and how can it be fixed?
I appreciate any help you guys can get me. :]
...creates frames, widgets, entries...
mainHealerVar = IntVar()
main_healer_check = Checkbutton(root, text="Enable the bot", variable=mainHealerVar) #this should mean that when i press this checkbutton it'll enable the bot and perform the repetitive commands for as long as it stays checked
... some more buttons/code...
root.mainloop() #right after I stop setting up the GUI, I place this function
... make logic in order to know which part of the repetitive functions to execute...
if mainHealerVar.get() == 1:
mainHealFunction() #function to check if the checkbutton is still pressed (if it is, it should execute mainHealFunction()
It was supposed to execute the repetitive commands by the pynput module as soon as i clicked on the enabling Check-button, but it doesn't execute anything until I close the GUI, then it starts to execute the repetitive commands. I have also tried using a while loop instead of an "if" at the end, but ended up with the same result.
To cut a long story short, I've been doing an interactive GUI (tkinter) word-game program for school. At first, everything went smoothly, but having finished the code, it has started to behave in unexpected ways when I run it. Some dialog boxes (particularly the
if tkinter.messagebox.askyesno():
thingy) just rapidly answer themselves with the 'no' option, rather than waiting for user input. Sometimes, the windows close off completely and cause the whole program to quit. However, although these errors are all the same (i.e. tkinter windows closing/answering themselves/stopping the program before they should), they usually happen in different places every time. I'm not sure if that's to do with the fact that tkinter is nested, opened, re-opened and closed numerous times within other code, which is making it run messily, but I have only destroyed tkinter windows in the right places, as far as I know.
Part of my code involves a while loop - I'm not sure if that could be interfering with the mainloop()s, but I couldn't find another way to allow the user to repeat the game as many times as they want.
I know this question is vague, but I'm mainly looking for tips - if it would be easier to diagnose if I split it up into different sections and tidy it up a bit, found an alternative for the while loop, etc.
Thanks!
TKinter dialogs should be fully completed and the results stored before moving onto the next section of code.
Make sure you provide all the arguments to the dialog (your example doesn't include the parameters).
result = tkinter.messagebox.askyesno('Confirm', 'Do you want to do this')
if result == true:
In Pygame, how can I get graphical input(e.g. clicking exit button) and also get input from the a terminal window simultaneously?
To give you context, my game has a GUI but gets its game commands from a "input()" command. How can I look for input from the command line while also handling graphics?
I'm not sure if this is possible, but if not, what other options do I have for getting text input from the user?
Thanks in advance.
You can't do that, unless you use the input command in a different thread, but then you have to deal with syncronization (which might be what you want or don't want to do).
The way I'd implement this is to create a kind of in-game console. When a special key (e.g. '\') is pressed you make the console appear, and when your application is in that state you interpreter key pressing not as in-game commands but... well, as text. You can print them in the console (using fonts). When a key (e.g "return") is pressed you can make the console disappear and the keys take back their primary functionality.
I did this for my pet-project and it works as a charm. Plus, since you are developing in python you can accept python instructions and use exec to execute them and edit your game "on fhe fly"