【问题标题】:How to make button in Python Tkinter stay pressed until another one is pressed如何使 Python Tkinter 中的按钮保持按下状态,直到按下另一个按钮
【发布时间】:2014-06-14 17:55:12
【问题描述】:

我正在尝试找到一种方法让 Tkinter 使 Start 按钮保持按下状态,直到我按下 Stop 按钮。

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

【问题讨论】:

    标签: python button user-interface tkinter


    【解决方案1】:

    所以你可以使用它的配置设置按钮的浮雕,这使它看起来像是被按下了。

    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

    【讨论】:

    • 哪一部分不起作用?我认为这里可能存在平台依赖性,因为这适用于我的 Linux 机器,但不适用于 Mac OSx。
    • "dosen't work" 信息量不是很大。发生什么了?你看到任何错误吗?这是complete code example to try
    • @J.F.Sebastian 在 MacOSx 上(至少在 10.7.5 和 python 2.7 上)按钮永远不会被按下并且无法设置救济状态。
    • @J.F.Sebastian 仍然没有,它会改变高亮颜色和文本颜色,但不会改变浮雕。
    • @J.F.Sebastian 再次没有。鉴于按钮在 Mac 上的呈现方式,我怀疑没有直接的方法可以做到这一点。
    【解决方案2】:

    如果您的系统上有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()
    

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多