我是tkinter/customtkinter的新手,我想尝试一下。我试图在按钮/标签上使用自定义字体,但当我为特定按钮尝试时,它不起作用。
注:大质量的";计算器按钮";在"#计算器按钮-M-frame-定义数字按钮";是无用的,除了定义一些变量之外什么都不做。
import tkinter
from tkinter.tix import COLUMN
from turtle import window_width
import customtkinter
import tkinter.font as tkfont
customtkinter.set_appearance_mode("dark") # Modes: system (default), light, dark
customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green
w = 780
h = 520
app = customtkinter.CTk() # create CTk window like you do with the Tk window
ws = app.winfo_screenwidth() # width of the screen
hs = app.winfo_screenheight() # height of the screen
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
app.geometry("%dx%d+%d+%d" % (w,h,x,y))
app.grid_rowconfigure(0,weight=1)
app.grid_columnconfigure(1,weight=1)
app.resizable(width=False, height=False)
font_Lframe = tkfont.Font(family='Arciform', size=12, weight='normal')
font_Mframe = tkfont.Font(family='Arciform', size=20, weight='normal')
def hide_event(event):
event.grid_forget()
def show_event(event):
event.grid(row=1, column=0, padx=10,pady=20)
def calc_addvalue(number):
pass
#L
L_frame = customtkinter.CTkFrame(master=app, width=180, corner_radius=0)
L_frame.grid(row=0, column=0, sticky="nsew")
#R
M_frame = customtkinter.CTkFrame(master=app, corner_radius=20)
M_frame.grid(row=0, column=1, sticky="nsew", padx=20, pady=20)
#side buttons
page1 = customtkinter.CTkButton(master=L_frame, text="button one", command=lambda: hide_event(page1), text_font=font_Lframe)
page1.grid(row=1, column=0, padx=10,pady=20)
page2 = customtkinter.CTkButton(master=L_frame, text="button two", command=lambda: show_event(page1), text_font=font_Lframe)
page2.grid(row=2, column=0, padx=10,pady=20)
page3 = customtkinter.CTkButton(master=L_frame, text="button three", text_font=font_Lframe)
page3.grid(row=3, column=0, padx=10,pady=20)
#calculator input - M_frame - defining input field
calc_labelvar = tkinter.StringVar(value="The quick brown fox jumps over the lazy dog")
calc_label = customtkinter.CTkLabel(master=M_frame, textvariable=calc_labelvar, width=250, height=100, corner_radius=20, fg_color="#464646", text_font=font_Mframe)
calc_label.grid(row = 3, column = 1, padx=0, pady=40)
#calculator buttons - M_frame - defining number buttons
calc_button1 = customtkinter.CTkButton(master=M_frame, text="1", command=calc_addvalue(1), text_font=font_Mframe)
calc_button2 = customtkinter.CTkButton(master=M_frame, text="2", command=calc_addvalue(2), text_font=font_Mframe)
calc_button3 = customtkinter.CTkButton(master=M_frame, text="3", command=calc_addvalue(3), text_font=font_Mframe)
calc_button4 = customtkinter.CTkButton(master=M_frame, text="4", command=calc_addvalue(4), text_font=font_Mframe)
calc_button5 = customtkinter.CTkButton(master=M_frame, text="5", command=calc_addvalue(5), text_font=font_Mframe)
calc_button6 = customtkinter.CTkButton(master=M_frame, text="6", command=calc_addvalue(6), text_font=font_Mframe)
calc_button7 = customtkinter.CTkButton(master=M_frame, text="7", command=calc_addvalue(7), text_font=font_Mframe)
calc_button8 = customtkinter.CTkButton(master=M_frame, text="8", command=calc_addvalue(8), text_font=font_Mframe)
calc_button9 = customtkinter.CTkButton(master=M_frame, text="9", command=calc_addvalue(9), text_font=font_Mframe)
calc_button0 = customtkinter.CTkButton(master=M_frame, text="0", command=calc_addvalue(0), text_font=font_Mframe)
**edit (as i also commented below):**
the code is weird. the bug does not just happen to the "font_Lframe" variable, but the "font_Mframe" variable too. someone in the comments stated it is not reproduceable, so i'll try it on my other computer later to see what happens. any suggestions are appreciated.
app.mainloop()
控制台/错误:
c:UsershiddenDocumentsprojectstkintertest.py:2: DeprecationWarning: The Tix Tk extension is unmaintained, and the tkinter.tix wrapper module is deprecated in favor of tkinter.ttk
from tkinter.tix import COLUMN
Traceback (most recent call last):
File "c:UsershiddenDocumentsprojectstkintertest.py", line 41, in <module>
page2 = customtkinter.CTkButton(master=L_frame, text="button two", command=lambda: show_event(page1), text_font=font_Lframe)
File "C:UsershiddenAppDataLocalProgramsPythonPython310libsite-packagescustomtkinterwidgetsctk_button.py", line 88, in __init__
self.draw()
File "C:UsershiddenAppDataLocalProgramsPythonPython310libsite-packagescustomtkinterwidgetsctk_button.py", line 141, in draw
font=self.apply_font_scaling(self.text_font),
File "C:UsershiddenAppDataLocalProgramsPythonPython310libsite-packagescustomtkinterwidgetswidget_base_class.py", line 225, in apply_font_scaling
if font.cget("size") < 0:
File "C:UsershiddenAppDataLocalProgramsPythonPython310libtkinterfont.py", line 143, in cget
return self._call("font", "config", self.name, "-"+option)
_tkinter.TclError: named font "font1" doesn't exist
新的错误输出(在定义字体变量时删除了"系列"one_answers"大小"部分(:
PS C:UsershiddenDocumentsprojects> & C:/Users/hidden/AppData/Local/Programs/Python/Python310/python.exe c:/Users/hidden/Documents/projects/tkintertest.py
c:UsershiddenDocumentsprojectstkintertest.py:2: DeprecationWarning: The Tix Tk extension is unmaintained, and the tkinter.tix wrapper module is deprecated in
favor of tkinter.ttk
from tkinter.tix import COLUMN
Traceback (most recent call last):
File "c:UsershiddenDocumentsprojectstkintertest.py", line 22, in <module>
font_Lframe = tkfont.Font('Arciform', 12)
File "C:UsershiddenAppDataLocalProgramsPythonPython310libtkinterfont.py", line 76, in __init__
font = tk.splitlist(tk.call("font", "actual", font))
AttributeError: 'str' object has no attribute 'splitlist'. Did you mean: 'splitlines'?
信贷acw1668
将
font_Lframe = tkfont.Font(family='Arciform', size=12, weight='normal')
更改为font_Lframe=('Arciform', 12)
。似乎在使用Font实例时出现了问题。与font_Mframe
相同
谢谢:(