*为了消除任何混乱,我使用的是macOS Catalina,Python 3.7.4和Tcl / Tk 8.6.9 *
我有一个项目使用ttkthemes中的ThemedTK使用“黑色”作为主题。我正在尝试修改按钮的样式。
但是,除了set_theme_advanced()方法之外,我无法找出如何修改主题的方法,该方法只能更改我正在阅读的here的颜色。我想将按钮文本更改为主题内的中心。但是,在解决此问题的过程中,我看到我无法在下面的示例代码中的任何一个窗口中修改按钮文本的放置(调整)。
如果查看此示例代码,您将看到在第一个窗口中,文本在按钮上左对齐,但颜色正确,但是在第二个窗口中(使用Tk),文本在所有文本中居中对齐按钮,而不是彩色的。两个窗口均未响应对齐样式,并且Tk窗口似乎未响应任何自定义样式。
# test-ttk-button-style.py
import tkinter as tk
from tkinter import ttk, NSEW
from ttkthemes import themed_tk as tkt
window1 = tkt.ThemedTk()
window1.get_themes()
window1.set_theme("black")
window1.title("First Window")
window1.geometry("+20+70")
window2 = tk.Tk()
window2.title("Second Window")
window2.geometry("+400+70")
ttkStyle = ttk.Style()
ttkStyle.configure("left.TButton", justify="left", background="green", foreground="white", font="Helvetica 12 bold")
ttkStyle.configure("right.TButton", justify="right", background="blue", foreground="white", font="Helvetica 12 bold")
ttkStyle.configure("center.TButton", justify="center", background="red", foreground="white", font="Helvetica 12 bold")
w1content = ttk.Frame(window1, padding=(12,12,12,12))
w1content.grid(row=0, column=0, sticky=NSEW)
w1btn1 = tkt.ttk.Button(w1content, text="First Button in Window 1", style="left.TButton")
w1btn1.grid(row=0, column=0, padx=30, pady=30)
w1btn2 = tkt.ttk.Button(w1content, text="Second Button in Window 1", width=25, style="right.TButton")
w1btn2.grid(row=1, column=0, padx=30, pady=30)
w1btn3 = tkt.ttk.Button(w1content, text="Third Button in Window 1", width=25, style="center.TButton")
w1btn3.grid(row=2, column=0, padx=30, pady=30)
w1btn4 = tkt.ttk.Button(w1content, text="Fourth Button in Window 1", width=25)
w1btn4.grid(row=3, column=0, padx=30, pady=30)
w2content = ttk.Frame(window2, padding=(12,12,12,12))
w2content.grid(row=0, column=0, sticky=NSEW)
w2btn1 = tkt.ttk.Button(w2content, text="First Button in Window 2", style="left.TButton")
w2btn1.grid(row=0, column=0, padx=30, pady=30)
w2btn2 = tkt.ttk.Button(w2content, text="Second Button in Window 2", width=25, style="right.TButton")
w2btn2.grid(row=1, column=0, padx=30, pady=30)
w2btn3 = tkt.ttk.Button(w2content, text="Third Button in Window 2", width=25, style="center.TButton")
w2btn3.grid(row=2, column=0, padx=30, pady=30)
w2btn4 = tkt.ttk.Button(w2content, text="Fourth Button in Window 2", width=25)
w2btn4.grid(row=3, column=0, padx=30, pady=30)
window1.mainloop()
在此先感谢您的帮助。
* * EDIT * * 阅读下面的答案后,我将代码分解为两个文件,以便没有两个主窗口。这确实改变了Window2的行为,但是样式仍然没有应用。然后,我仅使用Tk和ttk创建了第三个窗口,但仍然没有应用样式。
窗口1(设置了tkthemed和“ black”主题样式)
# test-ttk-button-style1.py
import tkinter as tk
from tkinter import ttk, NSEW
from ttkthemes import themed_tk as tkt
window1 = tkt.ThemedTk()
window1.get_themes()
window1.set_theme("black")
window1.title("First Window")
window1.geometry("+0+0")
ttkStyle = ttk.Style()
ttkStyle.configure("left.TButton", justify="left", background="green", foreground="white", font="Helvetica 12 bold")
ttkStyle.configure("right.TButton", justify="right", background="blue", foreground="white", font="Helvetica 20 bold")
ttkStyle.configure("center.TButton", justify="center", background="red", foreground="white", font="Helvetica 32 bold")
w1content = ttk.Frame(window1, padding=(12,12,12,12))
w1content.grid(row=0, column=0, sticky=NSEW)
w1btn1 = tkt.ttk.Button(w1content, text="First Button in Window 1", style="left.TButton")
w1btn1.grid(row=0, column=0, padx=30, pady=30)
w1btn2 = tkt.ttk.Button(w1content, text="Second Button in Window 1", width=25, style="right.TButton")
w1btn2.grid(row=1, column=0, padx=30, pady=30)
w1btn3 = tkt.ttk.Button(w1content, text="Third Button in Window 1", width=25, style="center.TButton")
w1btn3.grid(row=2, column=0, padx=30, pady=30)
w1btn4 = tkt.ttk.Button(w1content, text="Fourth Button in Window 1", width=25)
w1btn4.grid(row=3, column=0, padx=30, pady=30)
window1.mainloop()
窗口2(未设置主题样式的主题)
# test-ttk-button-style2.py
import tkinter as tk
from tkinter import ttk, NSEW
from ttkthemes import themed_tk as tkt
window2 = tk.Tk()
window2.title("Second Window")
ttkStyle = ttk.Style()
ttkStyle.configure("left.TButton", justify="left", background="green", foreground="white", font="Helvetica 12 bold")
ttkStyle.configure("right.TButton", justify="right", background="blue", foreground="white", font="Helvetica 20 bold")
ttkStyle.configure("center.TButton", justify="center", background="red", foreground="white", font="Helvetica 32 bold")
w2content = ttk.Frame(window2, padding=(12,12,12,12))
w2content.grid(row=0, column=0, sticky=NSEW)
w2btn1 = tkt.ttk.Button(w2content, text="First Button in Window 2", style="left.TButton")
w2btn1.grid(row=0, column=0, padx=30, pady=30)
w2btn2 = tkt.ttk.Button(w2content, text="Second Button in Window 2", width=25, style="right.TButton")
w2btn2.grid(row=1, column=0, padx=30, pady=30)
w2btn3 = tkt.ttk.Button(w2content, text="Third Button in Window 2", width=25, style="center.TButton")
w2btn3.grid(row=2, column=0, padx=30, pady=30)
w2btn4 = tkt.ttk.Button(w2content, text="Fourth Button in Window 2", width=25)
w2btn4.grid(row=3, column=0, padx=30, pady=30)
window2.mainloop()
#test-ttk-button-style3.py
import tkinter as tk
from tkinter import ttk, NSEW
window3 = tk.Tk()
window3.title("Third Window")
ttkStyle = ttk.Style()
ttkStyle.configure("left.TButton", justify="left", background="green", foreground="white", font="Helvetica 12 bold")
ttkStyle.configure("right.TButton", justify="right", background="blue", foreground="white", font="Helvetica 20 bold")
ttkStyle.configure("center.TButton", justify="center", background="red", foreground="white", font="Helvetica 32 bold")
w3content = ttk.Frame(window3, padding=(12,12,12,12))
w3content.grid(row=0, column=0, sticky=NSEW)
w3btn1 = ttk.Button(w3content, text="First Button in Window 3", style="left.TButton")
w3btn1.grid(row=0, column=0, padx=30, pady=30)
w3btn2 = ttk.Button(w3content, text="Second Button in Window 3", width=25, style="right.TButton")
w3btn2.grid(row=1, column=0, padx=30, pady=30)
w3btn3 = ttk.Button(w3content, text="Third Button in Window 3", width=25, style="center.TButton")
w3btn3.grid(row=2, column=0, padx=30, pady=30)
w3btn4 = ttk.Button(w3content, text="Fourth Button in Window 3", width=25)
w3btn4.grid(row=3, column=0, padx=30, pady=30)
window3.mainloop()
答案 0 :(得分:1)
您已经创建了两个根窗口(实例为tkinter.Tk
),将第二个窗口设置为Toplevel
小部件:
window2 = tk.Toplevel()
答案 1 :(得分:1)
第二个窗口正在使用系统小部件,并且由于Apple的人机界面指南(按钮上的文本不能左右对齐)而具有受限制的选项。
关于ttk风格的小部件,这是tkinter的一个小功能,target_status_code=302
指定多行标签的行为,justify
可能是您想要将单行标签移到侧面的方式。
anchor = 控制文本(或图像)在按钮中的位置。使用N,NE,E,SE,S,SW,W,NW或CENTER之一。默认值为CENTER。 (锚点/锚点)
justify = 定义如何对齐多行文本。使用LEFT,RIGHT或CENTER。默认值为CENTER。 (调整/调整)
答案 2 :(得分:0)
在windows2中,没有使用ThemedTk()的地方可以使用ttkStyle.theme_use('alt')
显示结果。
看起来像这样,
ttkStyle = ttk.Style()
ttkStyle.theme_use('alt')
参考:https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-theme-layer.html