如何创建 tkinter 切换按钮? [英] How to create a tkinter toggle button?
本文介绍了如何创建 tkinter 切换按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用 Python 2.7 中的 Tkinter 开发文本编辑器.我正在尝试实现的一项功能是夜间模式,用户可以在其中切换黑色背景和浅色背景,只需单击切换按钮即可从浅色切换到深色.
I've been working on a text editor using Tkinter in Python 2.7. A feature that I'm trying to implement is the Night Mode, where the user can toggle between a black background and a light one, that switches from light to dark with a click of the toggle button.
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
However, on running the code, it is always in the night mode and the toggle doesn't work. Help. Source Code: http://ideone.com/IVJuxX
推荐答案
背景和 fg 仅在 if 子句中设置.您还需要在 else
子句中设置它们:
The background and fg are set only in the if-clause. You need to set them also in the else
clause:
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')
这篇关于如何创建 tkinter 切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文