Python Tinker标签使用anurati字体给出错误

时间:2018-08-15 14:07:46

标签: python tkinter fonts label

我在Windows 10上使用anurati font。我尝试从tkinter调用它以恢复错误

我的代码是:

class Program
{
    static void Main(string[] args)
    {
        List<IApplicant> applicants = new List<IApplicant>
        {
            new Clerk {FirstName = "Nik", LastName = "Corey"},
            new Manager {FirstName = "Sue", LastName = "Storm"},
            new Executive {FirstName = "Nancy", LastName = "Roman"}
        };

        List<Employee> employees = new List<Employee>();

        foreach (var ap in applicants)
        {
            Employee employee = ap.AccountConvertor.Convert();

            employees.Add(employee);
        }

        foreach (var emp in employees)
        {
            Console.WriteLine($"{emp.FirstName} {emp.LastName}: {emp.EmailAddress} IsManager: {emp.IsManager} IsExecutive: {emp.IsExecutive}");
        }

        Console.ReadLine();
    }
}

public interface IApplicant
{
    string FirstName { get; set; }
    string LastName { get; set; }
    IAccountConvertor AccountConvertor { get; set; }
}

public interface IAccountConvertor
{
    Employee Convert();
}

错误是

from tkinter import *

root = Tk()
root.title("P.E.T.A.R")
txt = Label(root, text = "welcome to project petar")
txt.grid(column = 0, row = 0, font=("Anurati Regular"))

为什么会这样

1 个答案:

答案 0 :(得分:3)

您必须先渲染字体,而且使用不正确。

在开始时使用以下代码:

from tkinter import *
import tkinter.font
my_font = tkinter.font.Font(root,family="Anurati Regular")

然后您可以像使用它一样

txt = Label(root, text = "welcome to project petar",font=my_font)
txt.grid(column = 0, row = 0)

因此,您的总体代码如下:

from tkinter import *
import tkinter.font
root = Tk()
root.title("P.E.T.A.R")
my_font = tkinter.font.Font(root,family="Anurati Regular")
txt = Label(root, text = "welcome to project petar",font=my_font)
txt.grid(column = 0, row = 0)

编辑:

正如您在评论中说的this method does not create the font just a different version of the default那样,您使用错误的名称调用字体或未安装该字体,并且发生这种情况时,tkinter创建一种基本字体。为了证明此方法有效,我编写了另一个使用Windows内置字体的代码:

from tkinter import *
import tkinter.font
root = Tk()
root.title("P.E.T.A.R")
my_font = tkinter.font.Font(root,family="Comic Sans MS")
my_font2 = tkinter.font.Font(root,family="Copperplate Gothic Bold")
txt = Label(root, text = "welcome to project petar",font=my_font)
txt.grid(column = 0, row = 0)
txt2 = Label(root, text = "welcome to project petar",font=my_font2)
txt2.grid(column = 0, row = 1)

执行此代码时:

Workig

编辑2:

我作了进一步调查,下载了Anurati字体,最后意识到我是正确的。它有两个问题:

  • 您输入的姓名不正确。名称为Anurati,但您正在使用Anurati Regular。您应该使用my_font = tkinter.font.Font(root,family="Anurati")
  • 此字体的小写字母很简单,而大写字母不是。并且所写的文字是小写字母。您的文字应为txt = Label(root, text = "WELCOME TO PROJECT PETAR",font=my_font)

之后,您的最终代码将变为:

from tkinter import *
import tkinter.font
root = Tk()
root.title("P.E.T.A.R")
my_font = tkinter.font.Font(root,family="Anurati")
txt = Label(root, text = "WELCOME TO PROJECT PETAR",font=my_font)
txt.grid(row=0,column=0)

因此,运行上面的代码后,您将获得预期的输出: Anurati