Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make button in Python Tkinter stay pressed until another one is pressed

I am trying to find a way Tkinter to make the Start button stay pressed until I press the Stop button.

from Tkinter import *
import tkMessageBox


class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(250, 150)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        for i in range(2):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.button0 = Button(self, text="Start", command=self.save, activeforeground="red")
        self.button0.grid(row=0, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)

        self.button1 = Button(self, text="Stop", command=self.stop, activeforeground="red")
        self.button1.grid(row=1, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)

    def save(self):
        pass

    def stop(self):
        pass


if __name__=="__main__":
   d=MainWindow()
   d.mainloop()
like image 781
Gianni Spear Avatar asked Apr 29 '14 01:04

Gianni Spear


People also ask

How do you wait for a button press in Python?

In Python 2 use raw_input(): raw_input("Press Enter to continue...") This only waits for the user to press enter though. This should wait for a keypress.

How do you handle a button click in Python?

The Button widget is useful for handling such events. We can use Button widget to perform a certain task or event by passing the callback in the command. While giving the command to the Button widget, we can have an optional lambda or anonymous functions which interpret to ignore any errors in the program.

Can one button have two commands tkinter?

We can perform a certain action with the help of a Button that encapsulates the function and the objects. However, there might be cases when we want to perform multiple operations with a single button. This can be achieved by defining the lambda functions which target multiple events or callback in the application.

What is IntVar () in tkinter?

_ClassType IntVarConstruct an integer variable. set(self, value) Set the variable to value, converting booleans to integers. get(self) Return the value of the variable as an integer.


1 Answers

So you can set the relief of the button using its config, this makes it look like it is pressed.

def save(self):
    self.button0.config(relief=SUNKEN)
    # if you also want to disable it do:
    # self.button0.config(state=tk.DISABLED)
    #...

def stop(self):
    self.button0.config(relief=RAISED)
    # if it was disabled above, then here do:
    # self.button0.config(state=tk.ACTIVE)
    #...

EDIT

This doesn't work on Mac OSx apparently. This link shows how in should look: http://www.tutorialspoint.com/python/tk_relief.htm

like image 128
ebarr Avatar answered Oct 16 '22 13:10

ebarr