我在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"))
为什么会这样
答案 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)
执行此代码时:
我作了进一步调查,下载了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)