我试图改变我的字体大小,但仍然没有影响。我如何实际更改字体大小?

时间:2016-08-31 19:46:48

标签: python tkinter

我正在尝试更改Tkinter中按钮的字体大小,所以它不是那么小。有没有人知道我能做什么可能会产生我正在寻找的结果?为什么我需要这么多文字才能提出问题,当我可以简单地用简短的文字提问时呢?!

from Tkinter import *
from tkFont import *


class App:
    def __init__(self):
        guiWindow = Tkinter.Tk()
        guiWindow.wm_title("FooBar")

        # Creates a custom font
        customFont = Font(size=18)

        # code to add widgets will go here
        buttonFrame = Frame()

        # colors the "ChangeLicense" button
        color = '#005DA6'

        # tells licenseChange what to do
        def openLicenseChange():
            print('Change License')

    #button properties
        licenseChange = Button(guiWindow,
                               command=openLicenseChange,
                               bd=20, bg=color, font=customFont,
                               text="Change License")

        buttonFrame.pack(side="top", fill="x")

        guiWindow.mainloop()
app=App()

3 个答案:

答案 0 :(得分:0)

我不熟悉Font,但您也可以使用字符串表示它:

customFont = '{arial} 18'

默认字体

customFont = '{} 18'

答案 1 :(得分:0)

创建tk.button对象时,请使用font选项,并将字体样式指定为字符串,如下所示: "Arial 10 Bold"

我使用您的代码作为示例,并使字体Arial,大小18,粗体。

from Tkinter import *
from tkFont import *


class App:
    def __init__(self):
        guiWindow = Tkinter.Tk()
        guiWindow.wm_title("FooBar")

        # Creates a custom font
        customFont = "Arial 18 Bold"

        # code to add widgets will go here
        buttonFrame = Frame()

        # colors the "ChangeLicense" button
        color = '#005DA6'

        # tells licenseChange what to do
        def openLicenseChange():
            print('Change License')

    #button properties
        licenseChange = Button(guiWindow,
                               command=openLicenseChange,
                               bd=20, bg=color, font=customFont,
                               text="Change License")

        buttonFrame.pack(side="top", fill="x")
        licenseChange.pack()
        guiWindow.mainloop()
app=App()

如果这不起作用,请尝试引用字符串中的每个单词,而不是"Arial 18 Bold"变量中的"'Arial' '18' 'Bold'",而不是customFont

答案 2 :(得分:0)

您可以创建一个新字体,然后使用my_widget['font']=my_font将其应用到您的小部件,例如代码:

from Tkinter import *
from tkFont import *


class App:

    def __init__(self):
        guiWindow = Tkinter.Tk()
        guiWindow.wm_title("FooBar")

        # Creates a custom font
        customFont = Font(size=18)

        # code to add widgets will go here
        buttonFrame = Frame()

        # colors the "ChangeLicense" button
        color = '#005DA6'

        # tells licenseChange what to do
        def openLicenseChange():
            print('Change License')

        # button properties
        helv36 = Font(family='Helvetica', size=36, weight='bold')
        licenseChange = Button(guiWindow,
                               command=openLicenseChange,
                               bd=20, bg=color, font=customFont,
                               text="Change License")
        licenseChange['font'] = helv36
        buttonFrame.pack(side="top", fill="x")

        guiWindow.mainloop()

app = App()

您可以阅读有关tkFont.Font here

的更多信息