如何在Python Tkinter中制作按钮,使其保持按下状态直到按下另一个按钮

6

我正在尝试寻找一种Tkinter的方法,使得开始按钮一直保持按下状态,直到我按下停止按钮。

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个回答

9

你可以使用按钮的配置来设置其凸起,这样它看起来就像被按下了。

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上不能工作。 - ebarr
“doesn't work” 不是非常有信息量的描述。发生了什么?你看到任何错误吗?这里有一个完整的代码示例供尝试 - jfs
它使用.config.configure两种方法都失败了吗?我已经更新了gist - jfs
1
@J.F.Sebastian 仍然无法,它改变了突出显示的颜色和文本颜色,但没有改变浮雕效果。 - ebarr
1
@J.F.Sebastian 再次否定。我怀疑在 Mac 上呈现按钮的方式,没有直接的方法来实现这个。 - ebarr
显示剩余4条评论

4
如果您的系统上Tkinter.Button无法配置其relief属性,那么您可以尝试使用ttk.Button基于代码代替:
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()

1
我知道现在可能已经太晚了,你可能已经得到了解决方案,但这个答案对其他人可能有帮助。
解决方案是使用单选按钮。创建单选按钮的主要动机与您的问题相同。
以下是来自GeeksforGeeks网站的示例:
# Importing Tkinter module
from tkinter import *
# from tkinter.ttk import *

# Creating master Tkinter window
master = Tk()
master.geometry("175x175")

# Tkinter string variable
# able to store any string value
v = StringVar(master, "1")

# Dictionary to create multiple buttons
values = {"RadioButton 1" : "1",
        "RadioButton 2" : "2",
        "RadioButton 3" : "3",
        "RadioButton 4" : "4",
        "RadioButton 5" : "5"}

# Loop is used to create multiple Radiobuttons
# rather than creating each button separately
for (text, value) in values.items():
    Radiobutton(master, text = text, variable = v,
                value = value, indicator = 0,
                background = "light blue").pack(fill = X, ipady = 5)

# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

此外,您可以创建按钮功能并实现所需的输出。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接