I basically have questions being asked in a for loop and I'm trying to make each question wait for a response (aka a button to be clicked) before showing the next question..
I was wondering how I can get this done because the way I have it setup now is that each question will appear one after the other regardless of the response...
Note that a for loop won't let the gtk main loop do it's jobs and call the callback methods for the events you expect and your application will be unresponsive.
One way to do what you need is use a gtk.Assistant with one question per page in the assistant object and, possible, one last page with a summary of the results to the questions based on the expected responses.
Don't put it in a for loop, you need to show the next question as a reaction to an event.
I haven't worked with pygtk, but to make yourself an idea, if you had something like this:
for question in questions:
some_text_pane.set_text(question)
#wait until button pressed
you should have something like that:
question_pool = iter(questions)
def next_question(evt):
question = next(question_pool)
some_text_pane.set_text(question)
some_button.set_event_handler(next_question)
of course I don't know the GTK API, so don't take the method names literally.
Related
I have a question about the window.Update and window.read in PySimpleGui.
I see that when I use window.read, the programm didn't continue while i did not click on a button. But if I click on a button the window will do all the Update that i want until it will meet a new window.read or window.refresh.
My problem is that i want to do an window.update, stop during a few second, and do another winodw.update after. I try to use time.sleep but it didn't work it seems like all the line of programm is execute in same time.
So i want to know if it is possible to stop the programm during a few second between two window.read or if it is possible to "force" the window.read or window.refresh (skip them without click on a button).
Thank you in advance and sorry for my english who is probably bad...
I would like to ask how could I add dynamically some widgets in my application one by one and not all at once. Those widgets are added in a for loop which contains the add_widget() command, and is triggered by a button.
So I would like to know if there is a way for the output to be shown gradually, and not all at once, in the end of the execution. Initially I tried to add a delay inside the for loop, but I'm afraid it has to do with the way the output is built each time.
EDIT: Well, it seems that I hadn't understood well the use of Clock.schedule_interval and Clock.schedule_once, so what I had tried with them (or with time.sleep) didn't succeed at all. But obviously, this was the solution to my problem.
Use Clock.schedule_interval or Clock.schedule_once to schedule each iteration of the loop at your desired time spacing.
If there are lets say 4 buttons, all with the same Click event, how can I find out which button was pressed?
if the event looks like this def Button_Click(self, sender, e): I'm sure I can compare sender to my buttons somehow. But how?
Well, I've never used IronPython so I don't know how much help this will be, but what I usually do when trying to figure out these things in regular python is to print type(sender) , print sender and print dir(sender) to console(or output to a file if you don't have a console available).
This should help you figure out what exactly is the "sender" parameter. In the simplest case it could be the button itself so a simple == will work to know which button it was. Or it could have a method/property that gets you the button object. In which case, dir(sender) might contain an obvious one, or if not, google the class name gotten from type(sender) and see if you can find any docs.
I'm sure this question has been asked many times before, so I apologize in advance. I simply cannot find the answer via google or searching stack overflow.
I'm working in python with the wx library. I simply need a wx.EVT_CHAR to be thrown anytime a user presses a key, no matter the focus. How can this be accomplished? Is there a way to bind all widgets? Or a way to always throw an event when application receives a key press?
I tried binding the application itself, the main frame, and the main panel. None of these have accomplished the job of always throwing a wx.EVT_CHAR when a key is pressed.
I was able to solve my problem by writing a recursive method that sets every widget to receive characters and binds every widget to my callback function. It's pretty simple, but took me a bit of googling to realize that not every widget can inherently receive text events (such as a button). Hopefully this will save someone some time in the future. It should be noted that only widgets that are children or subchildren of the parent window passed will be bound to the callback method.
def __RecursiveBinding(self, parent):
try:
parent.Bind(wx.EVT_CHAR, self.CharInputCallback)
parent.SetWindowStyleFlag(wx.WANTS_CHARS)
parent.Refresh()
children = parent.GetChildren()
if(children):
for child in children:
self.__RecursiveBinding(child)
I've done a few searches but I couldn't find anything about this topic. Perhaps because it is common programmer knowledge (I'm not a programmer, I've learned from necessity), or because I'm going about it the wrong way.
I would like ideas/suggestions on how to manage button states for a GUI. For example, if I have a program which allows the user to import and process data, then certain functions should be inaccessible until the data has been imported successfully, or if they want to graph certain data, they need to select which data to graph before hitting the 'graph' or 'export' button. Even in the simple programs I've built these relationships seems to get complicated quickly. It seems simple to say "User shouldn't be able to hit button 'A' until 'B' and 'C' have been completed, then 'A' should be disabled if button 'D' or the 'Cancel' button. But that's a lot to track for one button. Thus far, I've tried two things:
Changing/Checking button states in the callback functions for the button. So in the above example, I would have code in buttons B's and C's callback to check if A should be enabled. And in buttons D's and Cancel's callbacks I would have code to disable button A. This gets complicated quickly and is difficult to maintain as code changes.
Setting boolean variables in every buttons callback (or just checking the states later using cget()) and checking the variables in a polling function to determine which buttons should be enabled or disabled.
I'm just not sure about this. I would like to make code as short and easy to understand as possible (and easy to edit later), but I don't like the idea of polling all the button states every few hundred milliseconds just for button 'management'. You can extend the same idea to check boxes, menu items, etc... but I'd like to here what others have done and why they do it the way they do.
You are only changing button states based on events, right? There is no reason to 'poll' to see if a button state has changed. What you can do is build a function which does all of the calling for you, then call it with something like disable_buttons([okButton, graphButton, printButton]). When an event takes place that modifies the appropriate user interface options (such as importing data), have another function that turns them on: enable_buttons([graphButton]). You could do this with each object's methods, of course, but making a wrapper allows you to be consistent throughout your application.