【问题标题】:I've tried to change my font size and it still has no affect. How do I actually change the font size?我试图改变我的字体大小,它仍然没有影响。我如何实际更改字体大小?
【发布时间】:2017-01-08 12:42:06
【问题描述】:

我正在尝试更改 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()

【问题讨论】:

标签: python tkinter


【解决方案1】:

创建tk.button 对象时,使用字体选项,并将字体样式指定为字符串,如下所示: "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()

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

【讨论】:

    【解决方案2】:

    您可以创建一个新字体,然后使用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

    【讨论】:

    • 我通过修改你给我的代码来尝试这个,但它没有工作。然后我尝试从字面上复制和粘贴,但仍然不起作用。我的字体仍然是默认字体。感谢您的意见,还有其他想法吗?
    • @AveryLipsit 你能详细说明一下吗?您能否为您的代码制作一个mcve,以便我能够诊断出发生了什么
    • 没问题!我什至没有意识到这是一件事谢谢!一会儿……
    • 我现在下班回家了...我会更新一点...对不起:(
    【解决方案3】:

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

    customFont = '{arial} 18'
    

    对于默认字体 uer

    customFont = '{} 18'
    

    【讨论】:

    • 我刚刚尝试过,并产生了与字体为默认大小相同的结果。为了确保字体不是 18,我将其更改为 32,结果仍然相同。还有其他想法吗?
    • 我没有试过你的代码,但是我看到你没有打包 licenseChange 按钮。你可以试试……
    • 嘿抱歉,我刚回去工作,可以看看这个。我注意到,在我对字体问题进行故障排除时,我一定也无意中删除了它。我的问题仍然存在 - 此解决方案是否适用于其他代码 sn-ps 或在您的机器上使用我的代码?我可能只是一个糟糕的编码员(可能是这种情况,因为这只是我第二次或第三次尝试编程)再次感谢您提供的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2013-06-20
    相关资源
    最近更新 更多