python font error 2x

Asked

Viewed 87 times

2

I am trying to create separate fonts for my project in a particular.py file, after creating them I care for the.py project, I do it this way as follows:

py.

"""
Criando as fontes.
"""
def fontes(self):
    self.font1=("Arial", "60")

py project.

 from Tkinter import *
 from fonte import *

 class base:
    def __init__(self, janela):
        caixa=Frame(janela)
        caixa.pack()
        a=Label(caixa, text="teste para o funcionamento das fontes", font=self.font1)
        a.pack()
        root=Tk()
        base(root)
        root.mainloop()

But when I try to use the source:

a = Label(caixa, text="teste para o funcionamento das fontes", font=font1)

She’s making a mistake and says she’s not global:

Nameerror: global name 'font1' is not defined

Could someone tell me how I can make use of sources without being within the project itself?

1 answer

1


The error is happening because the system is searching for the variable 'font1' which in your case was never declared.

You can do the following:

py.

import tkfont

def fontes(self):
    arial = Font(family="Arial",size=60,weight="normal") 
    return arial

py project.

...
a=Label(caixa, text="teste para o funcionamento das fontes", font=fontes())
...
  • Hi - so the code you put here should be ok (I haven’t tested, but it doesn’t look good -- come to think of it, pq. Does your "fonts" function have the "self" parameter?? It doesn’t look so good anymore... :-/) - but O.P. is having doubts about how Python works, confusing functions with classes, for example, using recognized bad practices (from X import * ) - what do you think about giving a touch on these things to improve your response. I.e.: clarifying what is wrong, not only posting some lines that work?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.