duten
Posts: 3
Joined: Mon May 10, 2021 4:24 pm

Issues converting program to work with GUIzero

Mon May 10, 2021 5:21 pm

Program Purpose:

Python 3
PyCharm IDE

Before I begin let me just say that I only started learning Python and coding a couple days ago so I'm aware I may have missed something in the learning process.

The intended function of this program is to allow someone with limited communication to be able to speak or have at least basic communication. What the program does is begin cycling between options automatically with a set time interval in between. This allows an option to be displayed long enough for the person to click a single button to select that option. That option would then be displayed. The whole premise is communication with just one button click or other single sensory input. This sample from the much larger program just asks the user "Yes?" or "No?" but the rest of the program functions using the same format. Currently, "No" is going to be selected for testing purposes because it's hardcoded in until I add the GPIO functionality.

The Problem:

The program does exactly what I need it to but now I'd like to make the text that is normally returned using print() in the IDE to be displayed on a 7" screen to make it more user friendly. Therefore, I need to make a GUI. All I need it to do is display the same text. However, from what I've been reading pretty much none of my program works with a GUI.
-Can't use while
-Can't use sleep.time

Which are the two most important functions of the program... I've tried to find alternatives that will work with something basic like tkinter or Guizero but nothing seems like a straightforward solution. I've looked at threading, and the after/repeat functions of Guizero but so far haven't gotten them to work.

What I need to solve to fix the issues:
-A different way to handle the automatic option cycling time interval
-A way to replace the while loop

Any help I can get on this would be very appreciated. In the meantime I'll keep grinding for a solution. Thanks!

Code: Select all

import time


def yesnomode():
    count = 0
    switch_state = 15  # Variable that holds live value
    switch_thresh = 10  # Variable to tune the threshold
    button_state = 0  # Variable that holds live value
    button_thresh = 10  # Variable to tune the threshold
    rate = 1.5  # Rate that the options will cycle
    print("Beginning Yes or No Mode")
    while switch_state >= switch_thresh:  # if switch reaches threshold then begin option cycling
        count = count + 1
        time.sleep(rate)  # This is the rate that the options will cycle
        if count == 1:
            print("Yes?")
            if button_state >= button_thresh:
                print("Yes Selected")
                time.sleep(rate)
        if count == 2:
            print("No?")
            button_state = 10
            if button_state >= button_thresh:
                time.sleep(rate)
                print("No Selected")
                button_state = 0
        if count == 3:
            count = 0


yesnomode()

User avatar
joelostinspace
Posts: 129
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Issues converting program to work with GUIzero

Wed May 12, 2021 2:50 am

I don't understand why you need to cycle the button, if the intent is allow someone with limited communication a choice, why make it difficult to time the pressing of a button? How about 4 static buttons Yes, No, Maybe, I don't know and a dynamic question.

If a little knowledge is dangerous, where is the man who has so much as to be out of danger?


--- Thomas Huxley

ghp
Posts: 1968
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Issues converting program to work with GUIzero

Wed May 12, 2021 7:46 am

Here a sample where a background task places text into a tkinter text box.
In the background task, you will place your button code. It is just a mock so far.

Code: Select all

import tkinter
import threading
import time

class Gui(tkinter.Frame):
    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent)   
         
        self.parent = parent
        
        self.initUI()
        parent.protocol("WM_DELETE_WINDOW", self.exitAction)
        self.count = 0
        self.state = "active"
        
    def exitAction(self):
        self.state = "stopped"
        self.parent.destroy()
    
    def initUI(self):
        self.parent.title("Logging Example")
        
         
        self.pack(fill=tkinter.BOTH, expand=1)
        
        self.text= tkinter.Text(self, font=(None, 20))
        self.text.grid(column=0, row=0, columnspan=6, rowspan=1)
        
    def add_text(self, line):  
        if self.state == "stopped": return  
        
        self.text.insert(tkinter.END, line+"\n")
        self.count += 1
        if self.count > 6:
            self.text.delete(1.0, 2.0)
            self.count -= 1
        
gui = None
doSomethingInBackground = None
        
class DoSomethingInBackground:
    def __init__(self):
        self.runIt = True
        self.executeThread = threading.Thread(target=self._run)
        self.executeThread.start()
        
    def stop(self):
        self.runIt = False
        self.executeThread.join()    
     
    def _delay(self, t):
        """shutdown should break long delay, so use this delay method instead of time.sleep"""
        for _ in range (0, int(t/0.1) ):
            time.sleep(0.1)
            if not self.runIt: break
                
    def _run(self):
        while self.runIt:
            self._delay(1)
            gui.add_text( "Yes?")

            self._delay(1)
            gui.add_text( "No?")
            
            self._delay(1)
            gui.add_text("No Selected")
                
def main():
    global gui
    global doSomethingInBackground

    try:    
        doSomethingInBackground = DoSomethingInBackground()
        
        root = tkinter.Tk()
        root.geometry("300x200+300+300")
        gui = Gui(root)
        
        root.mainloop()  
        
    except KeyboardInterrupt:
        pass
    finally:
        doSomethingInBackground.stop()
    
if __name__ == '__main__':
    main() 
    

duten
Posts: 3
Joined: Mon May 10, 2021 4:24 pm

Re: Issues converting program to work with GUIzero

Wed May 12, 2021 1:59 pm

joelostinspace wrote:
Wed May 12, 2021 2:50 am
I don't understand why you need to cycle the button, if the intent is allow someone with limited communication a choice, why make it difficult to time the pressing of a button? How about 4 static buttons Yes, No, Maybe, I don't know and a dynamic question.
The options should cycle automatically without input and the button selects it when it's displayed. However, the button is is an example I was using just to get the idea across. The input that actually makes these statements true is the result of a bunch of sensory information. The code I attached is simplified version of what I'm working with.
Last edited by duten on Wed May 12, 2021 2:18 pm, edited 1 time in total.

duten
Posts: 3
Joined: Mon May 10, 2021 4:24 pm

Re: Issues converting program to work with GUIzero

Wed May 12, 2021 2:02 pm

ghp wrote:
Wed May 12, 2021 7:46 am
Here a sample where a background task places text into a tkinter text box.
In the background task, you will place your button code. It is just a mock so far.

Thanks ghp :D , I'll take a look at this tonight. I'll post back if I get a working version going.

Return to “Assistive technology and accessibility”