-1

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()

2 Answers2

0

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()

Danhui Xu
  • 69
  • 10
  • It worked with that simple code, but it's not working on my app. :( @Danhui Xu – Omar Attia Aug 22 '22 at 03:01
  • Maybe you should not use ttk and just use normal tkinter, maybe this will can fix problems. – Danhui Xu Aug 22 '22 at 03:07
  • 1
    By the way, font('Arial',21) should be font=('Arial',21) – Danhui Xu Aug 22 '22 at 03:08
  • 1
    I think `import tkinter` and `fom tkinter import *` is unnesseccary to do this two thing at the same time. and `tkinter.NW` cn just be `NW` without imoprt tkinter – Danhui Xu Aug 22 '22 at 03:11
  • Yeah, I add an equal sign, but it is still not working. I imported tkinter in two ways because I was trying to figure out the problem is it in importing or something else. Actually, I accessed tkinter module to look if there's font in button option or not. I found there's no font option there. @Danhui Xu – Omar Attia Aug 22 '22 at 03:58
  • emmm..... i think the problem is not caused importing because `import tkinter` and `from tkinter import*` have the same effect.May be the error is caused by ttk? – Danhui Xu Aug 22 '22 at 04:17
  • And L've check that tkinter's button has font option,i think maybe can fix your error. – Danhui Xu Aug 22 '22 at 04:21
  • Alright, Thank You.@ Danhui Xu – Omar Attia Aug 22 '22 at 04:45
0

Edit: Re-wrote script and add image and tk.Photoimage.

I can't import font and anchor. I updated python, but it's still not working

I commented out the import font and from tkinter import. Actually, you don't need both.

Use import tkinter as tk then tk prefix.

Understanding how to to use ttk.Style(). For instance, ttk.Button(win, ...,style='Heading.TButton') #Heading.Tx .... x denote widget.

  • In line 7, add style = ttk.Style()
  • In line 9, add style.configure('Heading.TButton', font=('Arial', 21))
  • In line 12, remove keyword font=('Arial',21)
  • In line 18, add tk.Photoimage.
  • In line 19, add x, y coordinate of create_image.

Here is re-wrote for ttk:

from tkinter import ttk
import tkinter as tk

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

style.configure("Font.TButton", font=('Arial','21'))

l = tk.Label(win, text="Save File").pack()
btn = ttk.Button(text="Save", style="Font.TButton") #<== add style
btn.pack(pady=10)

f = ttk.Frame(win,width=200,height=200).place(x=50,y=275)
f_canvas = tk.Canvas(f,background='red',width=200,height=200)

img = tk.PhotoImage(file='p1.png') #<== add image
f_canvas.create_image(0, 10, image=img,anchor=tk.NW) #<== x, y coordinate
f_canvas.place(x=50,y=275)

win.mainloop()

Screenshot for ttk font and image:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19