简体   繁体   English

如何在 TkInter 的 function 中设置我的文本的字体大小和字体?

[英]How to set font size and font of my text within a function in TkInter?

I'm not sure what is wrong with me but I can't seem to figure out how to set the font size of my text within my "instructions" function.我不确定我出了什么问题,但我似乎无法弄清楚如何在我的“说明”function 中设置文本的字体大小。

import random
import time


num = random.randint(1, 100)


root = Tk()

root.title("Mack's totally advanced Number Guesser!")
root.geometry('404x300')
##root.resizable(False,False)
root.configure(bg='black')



fontA = 'BigNoodleTitling'
fontSizeA = 20



def instructions():
    text = StringVar()
    welcomeLbl.grid_forget()
    instrButton.grid_forget()
    startButton.grid_forget()
    insL = Label(root, textvariable=text, fg='pink', bg='black').grid(row=2, pady=100)
    text.set('So you need instructions eh?')


titleLbl = Label(root)
titleLbl['borderwidth'] = (2)
titleLbl['relief'] = ('solid')
titleLbl['text'] = ("Mack's totally advanced Number Guessing Game!")
titleLbl['font'] = (fontA, fontSizeA)
titleLbl.grid(row=0, column=0)


welcomeLbl = Label(root, fg='white', bg='black')
welcomeLbl['text'] = ("Welcome Random Person")
welcomeLbl['font'] = (fontA, 30)
welcomeLbl.grid(row=2, column=0, pady=20)

instrButton = Button(root, command=instructions)
instrButton['text'] = ("Instructions")
instrButton['font'] = (fontA, fontSizeA)
instrButton.grid(row=3,column=0, sticky=N)

startButton = Button(root, fg='red')
startButton['text'] = ("Start")
startButton['font'] = (fontA, 40)
startButton.grid(row=4,column=0, columnspan=1, sticky=E+W, pady=25)

Can anyone help me with setting the font and font size of my "So you need instructions Eh?"谁能帮我设置“所以你需要说明啊?”的字体和字体大小。 Thanks.谢谢。

When creating a label or button, or anything really with text in it, you can define the font and font size as follows在创建 label 或按钮或任何真正带有文本的内容时,您可以定义字体和字体大小如下

from tkinter import *

root = Tk()
label1 = Label(root, text = 'Hello' font = ('Calibri', 12))

When the first parameter for the 'font' function is the font name (as long as you have the font installed to your computer it should work) and the second parameter is the font size.当'font' function 的第一个参数是字体名称(只要您的计算机上安装了该字体,它应该可以工作),第二个参数是字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM