-1

I want to create Python GUI for desktop app. I designed this with Tkinter:

Two buttons are used to execute commands and exit the app, Button one is proceed to the code and Button two is for exit. But the GUI is not responding.

when executing the process Pressing button two does not exit the app, and pressing button 1 does not trigger the code in the function def clicked().

Is there any better method to exit even while running the program (like interrupts maybe?)

In Tkinter can we pass the value without pressing the button. (process is repeating but i need to press only one button press, need to execute automatically while showing the GUI)

def GUI():

   window =Tk()
   window.title("Select First Color")
   selected = IntVar()   

   rad1 = Radiobutton(window, text='RED', value=1, variable=selected)     
   rad2 = Radiobutton(window, text='YELLOW', value=2, variable=selected)

        def clicked():   
             # Go to a function according to the selection of radio button

   button1 = Button(window, text="Select", command=clicked)                    
   button2 = Button(window, text="Quit", command=window.destroy)

   rad1.grid(column=0, row=0)              # GUI RadioButton Placement
   rad2.grid(column=1, row=0)
   button1.grid(column=6, row=0)               # GUI Button Placement
   button2.grid(column=6, row=1)

window.mainloop()
ruohola
  • 21,987
  • 6
  • 62
  • 97
Sanjaya
  • 49
  • 1
  • 8
  • 1
    The question is not clear. Can you be more clear ? – Vineeth Sai Aug 28 '18 at 09:35
  • this is used for image processing. when red is selected from radio button it will detect only red areas, while yellow is detect only yellow area. but when it is difficult to exit (it is not responding) when the process is in `def Clicked():` lastly if the system detect area more than certain value, process should be repetitive according to the radio button selected – Sanjaya Aug 28 '18 at 09:40
  • If the program is not responding, You cannot exit it using the tkinter GUI i.e the buttons in the GUI. cause that will also be unresponsive. You must close it via _Ctrl + C_ or _Ctrl + Z_ in your terminal. If you just want to close the program with a button. You can change command of quit button to `sys.exit`. Don't forget to `import sys` too – Vineeth Sai Aug 28 '18 at 09:54

3 Answers3

2

Tkinter is single threaded, meaning it can only do one thing at a time. If your function clicked takes a long time, tkinter is not able to respond to events while it is running.

The only choices you have are to move the work done by clicked into a separate thread or process, or to refactor it so that it periodically calls the update method to give tkinter a chance to work through the event queue.

See this question for more help: Tkinter: How to use threads to preventing main event loop from "freezing"

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

1- you never call your GUI
2- variables defined inside a function have local scope, they cannot be accessed outside, except if you declare them in the global scope, and use global to change their values.

The following does what you expect:

when no radio button is selected, click prints 0
when the 'RED' radio button are selected, click prints 1
when the 'YELLOW' button are selected, click prints 2
You can use these values to redirect the call to another function.

import tkinter as tk


def do_nothing():
    pass


def do_red():
    print('doing the red thing')


def do_yellow():
    print('doing the yellow thing')


def clicked():
    print(f'clicked {selected.get()}')
    v = selected.get()
    if v == 0:
        do_nothing()
    elif v == 1:
        do_red()
    elif v == 2:
        do_yellow()
    else:
        print('an error occurred, the wrong value was recieved')


def GUI():

    global rad1, rad2, selected

    window = tk.Tk()
    window.title("Select First Color")
    selected = tk.IntVar()   

    rad1 = tk.Radiobutton(window, text='RED', value=1, variable=selected)     
    rad2 = tk.Radiobutton(window, text='YELLOW', value=2, variable=selected)

    button1 = tk.Button(window, text="Select", command=clicked)                    
    button2 = tk.Button(window, text="Quit", command=window.destroy)

    rad1.grid(column=0, row=0)
    rad2.grid(column=1, row=0)
    button1.grid(column=6, row=0)
    button2.grid(column=6, row=1)

    window.mainloop()


rad1, rad2, selected = None, None, None
GUI()

Alternatively, you can use classes in order to avoid the global variables:

import tkinter as tk


def do_nothing():
    pass


def do_red():
    print('doing the red thing')


def do_yellow():
    print('doing the yellow thing')


class GUI(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title("Select First Color")
        self.selected = tk.IntVar()
        self.selected.set(0)

        self.rad1 = tk.Radiobutton(self, text='RED', value=1, variable=self.selected)     
        self.rad2 = tk.Radiobutton(self, text='YELLOW', value=2, variable=self.selected)

        self.button1 = tk.Button(self, text="Select", command=self.clicked)                    
        self.button2 = tk.Button(self, text="Quit", command=self.destroy)

        self.rad1.grid(column=0, row=0)
        self.rad2.grid(column=1, row=0)
        self.button1.grid(column=6, row=0)
        self.button2.grid(column=6, row=1)

        self.mainloop()

    def clicked(self):
        print(f'clicked {self.selected.get()}')

        v = self.selected.get()
        if v == 0:
            do_nothing()
        elif v == 1:
            do_red()
        elif v == 2:
            do_yellow()
        else:
            print('an error occurred, the wrong value was received')


GUI()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • I am afraid the actual `clicked()` takes way more time to run than your made-up version. Add `time.sleep(60)` and see if you can close the window in less than a minute after clicking "Select".. – Stop harming Monica Aug 28 '18 at 12:36
  • Maybe, who knows? As it was posted, the GUI doesn't even start, let alone respond to an event... Further, whatever the `clicked` actions are were not posted, explained or described in any useful manner. – Reblochon Masque Aug 28 '18 at 12:47
  • @ReblochonMasque Thank you. sure and i'll explain next question in good manner. – Sanjaya Aug 28 '18 at 18:45
  • @ReblochonMasque Thank you, i voted up for you but unfortunately, my vote is not considered due low reputation (since new to the community) – Sanjaya Aug 29 '18 at 03:32
0

I think this is what you're looking for

button2 = Button(window, text="Quit", command=sys.exit)
ruohola
  • 21,987
  • 6
  • 62
  • 97