在简单的Python Tkinter程序中使用开/关函数实现暗模式?

在简单的Python Tkinter程序中使用开/关函数实现暗模式?,python,tkinter,Python,Tkinter,我按照本教程使用Python的Tkinter创建了一个非常简单的文本编辑器应用程序。我想做的是添加使用检查按钮的选项,这样当选中时,文本编辑器的主题将更改为暗模式主题,当未选中时,将返回默认的白色主题。我该怎么做 我尝试将一个函数绑定到checkbutton,它将检查状态,并根据状态更改窗口中帧的变量。例如,如果是: frame = tk.Frame(colour=white) 默认情况下,在函数中,我将: frame = tk.Frame(colour=white) 即使对我来说,这看起来

我按照本教程使用Python的Tkinter创建了一个非常简单的文本编辑器应用程序。我想做的是添加使用
检查按钮的选项,这样当选中
时,文本编辑器的主题将更改为暗模式主题,当
未选中时,将返回默认的白色主题。我该怎么做

我尝试将一个函数绑定到
checkbutton
,它将检查状态,并根据状态更改窗口中帧的变量。例如,如果是:

frame = tk.Frame(colour=white)
默认情况下,在函数中,我将:

frame = tk.Frame(colour=white)
即使对我来说,这看起来也不对。(我知道格式不正确。)

下面是代码(我没有尝试使用黑暗模式):

将tkinter作为tk导入
从tkinter.filedialog导入askopenfilename、asksaveasfilename
def open_文件():
“”“打开一个文件进行编辑。”“”
filepath=askopenfilename(
文件类型=[(“文本文件”,“*.txt”),(“所有文件”,“**”)]
)
如果不是文件路径:
返回
txt_edit.delete(1.0,tk.END)
以open(filepath,“r”)作为输入文件:
text=input_file.read()
txt_edit.insert(tk.END,text)
标题(f“简单文本编辑器-{filepath}”)
def save_file():
“”“将当前文件另存为新文件。”“”
filepath=asksaveasfilename(
defaultextension=“txt”,
文件类型=[(“文本文件”,“*.txt”),(“所有文件”,“**”)],
)
如果不是文件路径:
返回
将open(filepath,“w”)作为输出文件:
text=txt_edit.get(1.0,tk.END)
输出_文件。写入(文本)
标题(f“简单文本编辑器-{filepath}”)
window=tk.tk()
窗口标题(“简单文本编辑器”)
window.rowconfigure(0,minsize=800,weight=1)
window.columnconfigure(1,最小尺寸=800,重量=1)
txt_edit=tk.Text(窗口)
fr_按钮=tk.框架(窗口,浮雕=tk.凸起,bd=2)
btn_open=tk.Button(fr_按钮,text=“open”,command=open_文件)
btn_save=tk.Button(fr_按钮,text=“另存为…”,command=save_文件)
btn_open.grid(行=0,列=0,sticky=“ew”,padx=5,pady=5)
btn_save.grid(行=1,列=0,sticky=“ew”,padx=5)
fr_buttons.grid(行=0,列=0,sticky=“ns”)
txt_edit.grid(行=0,列=1,sticky=“nsew”)
window.mainloop()

您可以通过安装模块TTK主题来完成

import tkinter as tk
import tkinter.ttk as ttk 
from ttkthemes import ThemedStyle

app = tk.Tk()
app.geometry("200x400")
app.title("Changing Themes")
# Setting Theme
style = ThemedStyle(app)
style.set_theme("scidgrey")

# Button Widgets
Def_Btn = tk.Button(app,text='Default Button')
Def_Btn.pack()
Themed_Btn = ttk.Button(app,text='Themed button')
Themed_Btn.pack()

# Scrollbar Widgets
Def_Scrollbar = tk.Scrollbar(app)
Def_Scrollbar.pack(side='right',fill='y')
Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
Themed_Scrollbar.pack(side='top',fill='x')

# Entry Widgets
Def_Entry = tk.Entry(app)
Def_Entry.pack()
Themed_Entry = ttk.Entry(app)
Themed_Entry.pack()

app.mainloop()
pip安装ttkthemes

import tkinter as tk
import tkinter.ttk as ttk 
from ttkthemes import ThemedStyle

app = tk.Tk()
app.geometry("200x400")
app.title("Changing Themes")
# Setting Theme
style = ThemedStyle(app)
style.set_theme("scidgrey")

# Button Widgets
Def_Btn = tk.Button(app,text='Default Button')
Def_Btn.pack()
Themed_Btn = ttk.Button(app,text='Themed button')
Themed_Btn.pack()

# Scrollbar Widgets
Def_Scrollbar = tk.Scrollbar(app)
Def_Scrollbar.pack(side='right',fill='y')
Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
Themed_Scrollbar.pack(side='top',fill='x')

# Entry Widgets
Def_Entry = tk.Entry(app)
Def_Entry.pack()
Themed_Entry = ttk.Entry(app)
Themed_Entry.pack()

app.mainloop()

您可以通过安装模块TTK主题来完成

import tkinter as tk
import tkinter.ttk as ttk 
from ttkthemes import ThemedStyle

app = tk.Tk()
app.geometry("200x400")
app.title("Changing Themes")
# Setting Theme
style = ThemedStyle(app)
style.set_theme("scidgrey")

# Button Widgets
Def_Btn = tk.Button(app,text='Default Button')
Def_Btn.pack()
Themed_Btn = ttk.Button(app,text='Themed button')
Themed_Btn.pack()

# Scrollbar Widgets
Def_Scrollbar = tk.Scrollbar(app)
Def_Scrollbar.pack(side='right',fill='y')
Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
Themed_Scrollbar.pack(side='top',fill='x')

# Entry Widgets
Def_Entry = tk.Entry(app)
Def_Entry.pack()
Themed_Entry = ttk.Entry(app)
Themed_Entry.pack()

app.mainloop()
pip安装ttkthemes

import tkinter as tk
import tkinter.ttk as ttk 
from ttkthemes import ThemedStyle

app = tk.Tk()
app.geometry("200x400")
app.title("Changing Themes")
# Setting Theme
style = ThemedStyle(app)
style.set_theme("scidgrey")

# Button Widgets
Def_Btn = tk.Button(app,text='Default Button')
Def_Btn.pack()
Themed_Btn = ttk.Button(app,text='Themed button')
Themed_Btn.pack()

# Scrollbar Widgets
Def_Scrollbar = tk.Scrollbar(app)
Def_Scrollbar.pack(side='right',fill='y')
Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
Themed_Scrollbar.pack(side='top',fill='x')

# Entry Widgets
Def_Entry = tk.Entry(app)
Def_Entry.pack()
Themed_Entry = ttk.Entry(app)
Themed_Entry.pack()

app.mainloop()

这回答了你的问题吗?这回答了你的问题吗?我以为他想在黑暗和正常之间切换mode@CoolCloud好的:)我以为他想在黑暗和正常之间切换mode@CoolCloud好的:)