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

6 投票
3 回答
20622 浏览
提问于 2025-04-18 04:40

我想找个方法,让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 个回答

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

此外,你可以创建按钮功能,达到你想要的效果。

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

你可以通过按钮的配置来设置它的凹凸效果,这样按钮看起来就像被按下去了。

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

撰写回答