2014-04-29 414 views
2

我想在tkinter中找到一個方法,按鈕星星保持按下,直到我按下strop按鈕。在tkinter中的Python方法,按鈕保持按下,直到不同的命令

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() 

回答

3

因此,您可以使用其配置設置按鈕的浮雕,這使得它看起來像被按下。

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) 
    #... 

編輯

這並不在Mac OSX上工作顯然。此鏈接顯示如何應該看:http://www.tutorialspoint.com/python/tk_relief.htm

+0

這麼想的工作,對不起 –

+0

哪一部分不工作?我認爲這裏可能存在平臺依賴性,因爲這可以在我的Linux機器上運行,但不適用於Mac OSx。 – ebarr

+0

「dosen't work」是不是很豐富。怎麼了?你有沒有看到任何錯誤?這是一個[完整的代碼示例來嘗試](https://gist.github.com/11390111) – jfs

3

如果Tkinter.Button doesn't allow to configure its relief property您的系統上,那麼你可以嘗試ttk.Button-based code代替:

try: 
    import Tkinter as tk 
    import ttk 
except ImportError: # Python 3 
    import tkinter as tk 
    import tkinter.ttk as ttk 

SUNKABLE_BUTTON = 'SunkableButton.TButton' 

root = tk.Tk() 
root.geometry("400x300") 
style = ttk.Style() 

def start(): 
    button.state(['pressed', 'disabled']) 
    style.configure(SUNKABLE_BUTTON, relief=tk.SUNKEN, foreground='green') 

def stop(): 
    button.state(['!pressed', '!disabled']) 
    style.configure(SUNKABLE_BUTTON, relief=tk.RAISED, foreground='red') 

button = ttk.Button(root, text ="Start", command=start, style=SUNKABLE_BUTTON) 
button.pack(fill=tk.BOTH, expand=True) 
ttk.Button(root, text="Stop", command=stop).pack(fill=tk.BOTH, expand=True) 
root.mainloop()