Home > Enterprise >  I can't import font and anchor. I updated python, but it's still not working. It's no
I can't import font and anchor. I updated python, but it's still not working. It's no

Time:08-24

Can't import alot of stuff from tkinter, like Font and anchor

from tkinter import*
from tkinter import font
from tkinter import ttk
import tkinter
win= Tk()
win.geometry("300x550 970 45")
win.title("Gui")
l= Label(text="Save File").pack()
btn= ttk.Button(text= "Save",font('Arial',21)) # can't import font, I changed to font.Font and still not working
btn.pack(pady=10)
f= ttk.Frame(win,width=200,height=200).place(x=50,y=275)
f_canvas = Canvas(f,background='red',width=200,height=200)
f_canvas.create_image(0,0,anchor=tkinter.NW) # can't import anchor, even if I added a pic
f_canvas.place(x=50,y=275)
win.mainloop()

CodePudding user response:

i tkink the error is caused by tkk, when i turn btn= ttk.Button(text= "Save",font('Arial',21)) to btn = Button(text="Save", font=('Arial', 21)) There is no error any more and the font is working at any time.You also don't need to import font.it's unneccessnary, it's already in tkinter.

here is the code without error:

from tkinter import *
from tkinter import font
from tkinter import ttk
import tkinter

win = Tk()
win.geometry("300x550 970 45")
win.title("Gui")
l = Label(text="Save File").pack()
btn = Button(text="Save", font=('Arial', 21))
btn.pack(pady=10)
f = ttk.Frame(win, width=200, height=200).place(x=50, y=275)
f_canvas = Canvas(f, background='red', width=200, height=200)
f_canvas.create_image(0, 0, anchor=tkinter.NW)  # can't import anchor, even if I added a pic
f_canvas.place(x=50, y=275)
win.mainloop()

CodePudding user response:

I commented out the import font. Actually, you don't need this.

  • I add style = ttk.Style()
  • I remove font=('Arial',21)
  • I add style.configure('save', font=('Arial',21))

Here is code:

from tkinter import*
#from tkinter import font
from tkinter import ttk
import tkinter


win= Tk()
win.geometry("300x550 970 45")
win.title("Gui")
style = ttk.Style()

l= Label(text="Save File").pack()
btn= ttk.Button(text= "Save") # can't import font, I changed to font.Font and still not working
style.configure('save', font=('Arial',21))
btn.pack(pady=10)

f= ttk.Frame(win,width=200,height=200).place(x=50,y=275)
f_canvas = Canvas(f,background='red',width=200,height=200)
f_canvas.create_image(0,0,anchor=tkinter.NW) # can't import anchor, even if I added a pic
f_canvas.place(x=50,y=275)

win.mainloop()

Output image:

enter image description here

  • Related