如何创建tkinter切换按钮?
我一直在用Python 2.7和Tkinter做一个文本编辑器。现在我想加一个夜间模式的功能,也就是用户可以通过一个按钮在黑色背景和白色背景之间切换,只要点击一下按钮就能从亮色变成暗色。
from Tkinter import *
from tkSimpleDialog import askstring
from tkFileDialog import asksaveasfilename
from tkFileDialog import askopenfilename
from tkMessageBox import askokcancel
Window = Tk()
Window.title("TekstEDIT")
index = 0
class Editor(ScrolledText):
Button(frm, text='Night-Mode', command=self.onNightMode).pack(side=LEFT)
def onNightMode(self):
if index:
self.text.config(font=('courier', 12, 'normal'), background='black', fg='green')
else:
self.text.config(font=('courier', 12, 'normal'))
index = not index
但是,当我运行代码的时候,它总是处于夜间模式,那个切换按钮根本不起作用。求助!源代码在这里:http://ideone.com/IVJuxX
4 个回答
1
Albe的回答很好,但里面有一些不太好的编码习惯。
按照相同的步骤:
Import Tkinter as tk
top = tk.TK()
在这里定义你的函数,让它能适用于任何按钮,而不是死死绑定到你可能用的特定按钮上。
def toggle(button: tk.Button):
if button.config('relief')[-1] == 'sunken':
button.config(relief="raised")
else:
button.config(relief="sunken")
然后创建并放置你想要的所有切换按钮。
toggleButton = tk.Button(text="Toggle", width=12, relief="sunken",
command =lambda:toggle(toggleButton))
toggleButton.pack(pady=5)
top.mainloop()
这样做有两个好处。创建按钮对象两次是多余的,会导致代码出问题。把按钮死死绑定到特定的切换功能上是不灵活的。这个解决方案让代码可以重复使用,而且更容易添加功能。例如,把最后那段替换成:
for _ in range(4):
b = tk.Button(text="Toggle", width=12, relief="sunken")
b['command']= lambda a=b:toggle(a)
b.pack(pady=5)
现在你可以得到4个切换按钮,而不需要额外的函数或复制粘贴。
1
这里有一段代码,可以帮助你实现切换按钮的动画效果。如果你想要的话,只需要在代码中添加你希望在点击时执行的功能,具体内容就看你自己了。
'''
import tkinter as tk
# --- functions ---
def move(steps=10, distance=0.1):
if steps > 0:
# get current position
relx = float(frame.place_info()['relx'])
# set new position
frame.place_configure(relx=relx+distance)
# repeate it after 10ms
root.after(10, move, steps-1, distance)
def toggle(event):
if button["text"] == "Yes":
move(25, 0.02) # 50*0.02 = 1
button["text"] = "No"
print("Clicked on yes")
elif button["text"] == "No":
move(25, -0.02)
button["text"] = "Yes"
print("Clicked on no")
# --- main --
root = tk.Tk()
frame = tk.Frame(root, background='red')
frame.place(relx=0, rely=0, relwidth=0.5, relheight=1)
# to center label and button
#frame.grid_columnconfigure(0, weight=1)
#frame.grid_rowconfigure(0, weight=1)
#frame.grid_rowconfigure(3, weight=1)
button = tk.Button(frame, text='Yes',width=5,height=1)
button.place(relx=0.25,rely=0.5,relwidth=0.5, relheight=0.1)
button.bind("<Button-1>",toggle)
root.mainloop()
4
你可以导入tkinter库(在Python 2.7中要用大写字母):
import Tkinter
创建tkinter对象……
root = tk.Tk()
……以及tkinter按钮
toggle_btn = tk.Button(text="Toggle", width=12, relief="raised")
toggle_btn.pack(pady=5)
root.mainloop()
现在创建一个新的命令按钮,叫做“toggle”,这样当你按下按钮时,就能实现“切换”的效果,改变它的外观(凹陷或凸起):
def toggle():
if toggle_btn.config('relief')[-1] == 'sunken':
toggle_btn.config(relief="raised")
else:
toggle_btn.config(relief="sunken")
最后,把这个行为应用到你的按钮上:
toggle_btn = tk.Button(text="Toggle", width=12, relief="raised", command=toggle)
2
背景和前景颜色只在if条件里设置了。你还需要在else
条件里也设置它们:
def onNightMode(self):
if index:
self.text.config(font=('courier', 12, 'normal'), background='black', fg='green')
else:
self.text.config(font=('courier', 12, 'normal'))
index = not index
也就是说,
else:
self.text.config(font=('courier', 12, 'normal'), background='green', fg='black')