简体   繁体   中英

What is this "ghost" error? (tkinter/customtkinter fonts)

I'm new to tkinter/customtkinter, and I wanted to try it out. I was trying to use custom fonts with my buttons/labels, but when I try it for a specific button, it does not work.

note: the large mass of "calculator buttons" underneath the "#calculator buttons - M_frame - defining number buttons" is useless and does not do anything except define some variables.

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:

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

new error output (removed the 'family' and 'size' parts when defining the font variables):

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'?

Credit acw1668

Change font_Lframe = tkfont.Font(family='Arciform', size=12, weight='normal') to font_Lframe=('Arciform', 12) . It seems like there is issue when using instance of Font. Same for font_Mframe

thanks:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM