如何解决我在tkinter文本小部件中内置的黑暗模式不起作用
实际上,我正在构建一个记事本,我做了一个按钮,该按钮可以相互更改文本小部件的颜色。但是它不起作用,也不给出错误。有人可以解释为什么这不适用于解决方案吗?
T
解决方法
只需准备两种模式并将它们用作kwargs
。我让您开始的不仅仅是前景和背景色。
import tkinter as tk
root = tk.Tk()
root.geometry('450x500')
root.configure(bg='grey60')
root.title('my notepad')
#prepare base kwargs
common = dict(
font = 'arial 18 bold',borderwidth = 0,wrap = 'word',#or 'char','none'
relief = 'flat',tabs = '1c',highlightthickness = 2,insertwidth = 4,#fat caret
insertofftime = 600,#caret slow blink
insertontime = 1200,padx = 10,#thick padding
pady = 10,blockcursor = False #make True for really fat cursor
)
#prepare light kwargs
lightmode = dict(
foreground = 'black',background = 'white',insertbackground = 'black',#caret color
selectforeground = 'white',#when text is selected
selectbackground = 'black',#~
highlightbackground = 'gray40',#highlight color when Text does NOT have focus
highlightcolor = 'gray80',#highlight color when Text has focus
)
#prepare dark kwargs
darkmode = dict(
foreground = 'white',background = 'black',insertbackground = 'white',#caret color
selectforeground = 'black',#when text is selected
selectbackground = 'white',#~
highlightbackground = 'gray80',#highlight color when Text does NOT have focus
highlightcolor = 'gray40',#highlight color when Text has focus
)
#not used but acts as an example
#that you can just keep copy/pasting these things
#and changing values
mcdonaldsmode = dict(
foreground = 'yellow',background = 'red',insertbackground = 'yellow',#caret color
selectforeground = 'red',#when text is selected
selectbackground = 'yellow',#~
highlightbackground = 'red',#highlight color when Text does NOT have focus
highlightcolor = 'yellow',#highlight color when Text has focus
)
#toggle modes
def toggle():
text.basemode = not text.basemode
text.configure(**lightmode) if text.basemode else text.configure(**darkmode)
tk.Button(root,text='Switch Theme',bg='grey60',fg='grey100',command=toggle).pack(anchor='e')
text = tk.Text(root,**lightmode,**common)
text.basemode = True #invent a switch to toggle
text.pack()
root.mainloop()
,
您应该配置小部件,而不是编辑变量fg_col,bg_col
:
def darkmode():
text.config(foreground="white",background='black')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。