首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个“鬼”错误是什么?(特征码/定制字体)

这个“鬼”错误是什么?(特征码/定制字体)
EN

Stack Overflow用户
提问于 2022-08-23 14:50:15
回答 1查看 170关注 0票数 0

我是新手/定制者,我想试一试。我试图在按钮/标签上使用自定义字体,但当我尝试使用特定按钮时,它不起作用。

注意:在“#计算器按钮-定义数字按钮”下面的大量“计算器按钮”是无用的,除了定义一些变量之外,什么也不做。

代码语言:javascript
复制
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()

console/error:

代码语言:javascript
复制
c:\Users\hidden\Documents\projects\tkintertest.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:\Users\hidden\Documents\projects\tkintertest.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:\Users\hidden\AppData\Local\Programs\Python\Python310\lib\site-packages\customtkinter\widgets\ctk_button.py", line 88, in __init__
    self.draw()
  File "C:\Users\hidden\AppData\Local\Programs\Python\Python310\lib\site-packages\customtkinter\widgets\ctk_button.py", line 141, in draw
    font=self.apply_font_scaling(self.text_font),
  File "C:\Users\hidden\AppData\Local\Programs\Python\Python310\lib\site-packages\customtkinter\widgets\widget_base_class.py", line 225, in apply_font_scaling
    if font.cget("size") < 0:
  File "C:\Users\hidden\AppData\Local\Programs\Python\Python310\lib\tkinter\font.py", line 143, in cget
    return self._call("font", "config", self.name, "-"+option)
_tkinter.TclError: named font "font1" doesn't exist

新错误输出(在定义字体变量时删除了“系列”和“大小”部分):

代码语言:javascript
复制
PS C:\Users\hidden\Documents\projects> & C:/Users/hidden/AppData/Local/Programs/Python/Python310/python.exe c:/Users/hidden/Documents/projects/tkintertest.py
c:\Users\hidden\Documents\projects\tkintertest.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:\Users\hidden\Documents\projects\tkintertest.py", line 22, in <module>
    font_Lframe = tkfont.Font('Arciform', 12)
  File "C:\Users\hidden\AppData\Local\Programs\Python\Python310\lib\tkinter\font.py", line 76, in __init__
    font = tk.splitlist(tk.call("font", "actual", font))
AttributeError: 'str' object has no attribute 'splitlist'. Did you mean: 'splitlines'?
EN

回答 1

Stack Overflow用户

发布于 2022-08-23 16:19:35

信用acw1668

font_Lframe = tkfont.Font(family='Arciform', size=12, weight='normal')改为font_Lframe=('Arciform', 12)。在使用Font实例时似乎存在问题。同样适用于font_Mframe

谢谢:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73460847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档