Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asyncio semaphore problem
#1
I'm trying to wait for a tkinter dialog response to complete to get the results. I'm getting what seems to be a common problem but none of the threads I found seem to apply.
self.lock=asyncio.Lock()
self.answer=None
.
.
.
# start the dialog and wait for the response to be set
self.dialog(self.root,'HTTP Port',self.xml.getHTTPPort())
print('finally ',self.answer)
.
.
.

async def dialog(self,master,text,default):
    async with self.lock:
#call the function that does the tkinter dialog and wait for the answer to be set
        functions.dialog(master,text,default)
        print('dialog',self.answer)

async def setAnswer(self,ans):
# called from the tkinter dialog code which sets the answer and releases the lock
    self.answer=ans
    self.lock.release()
When I run this I get the error:

RuntimeWarning: coroutine 'frontPanel.dialog' was never awaited

Can someone tell me what I am doing wrong? TIA.
Reply
#2
GUI code should be event driven. The user does something or a timer expires or some other event occurs and the application executes some code (quickly) and returns to doing nothing. Eventually a different event occurs and a different bit of code executes. Your program should draw the dialog when the user makes a menu selection or presses a button. After the dialog is drawn, that code is done. There should be another bit of code that executes when the user has finished with the dialog. These are two different bits of code, not a code that draws the dialog and then waits for the dialog completion.
Reply
#3
Thanks for the reply but I don't think I follow. The code that calls this dialog is a thread that manages the main/root dialog. The user clicks on a button which triggers the sub-dialog but at that point I need to wait for a response from the user in order to update/continue the root dialog. I don't see any other way to accomplish that goal.
Reply
#4
Never mind. I don't need semaphores at all. I just found the magical tkinter method wait_window.
Reply
#5
Lets say your program needs to do A, B, C and D. To do C you need to present a dialog for input. You want to draw the dialog after B, wait for the user to respond, then continue on doing C and D. What I would do is call the dialog and end the thread of execution. Then I would have the dialog call a function to C and D.

As a more concrete example, let's say I want to open a file in my text editor program. Clicking on the File->Open menu option draws a file selection dialog. Once the dialog is draw, the code associated with asking to open a file is complete. The user can select a file, or close the dialog, or go get some coffee and not do anything for an hour. To the program it makes no difference. Nobody is waiting and there is no expectation. What the user does next will direct what the program does. The code to be executed is tied to the user action, not the other way around. If the user selects a file, the dialog callback will open the file and load the text into the editor. If the user closes the file dialog without making a selection, not only does the program not load the file, but the program doesn't even know there was an expectation of opening the file.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020