Pot*_*ead 6 python tkinter python-2.x toggle
我一直在使用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
Run Code Online (Sandbox Code Playgroud)
但是,在运行代码时,它始终处于夜间模式,并且切换不起作用.救命.源代码:http://ideone.com/IVJuxX
背景和 fg 仅在 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
Run Code Online (Sandbox Code Playgroud)
IE,
else:
self.text.config(font=('courier', 12, 'normal'), background='green', fg='black')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15539 次 |
| 最近记录: |