israel-dryer / ttkbootstrap

A supercharged theme extension for tkinter that enables on-demand modern flat style themes inspired by Bootstrap.
MIT License
1.79k stars 363 forks source link

Button control does not support font property #468

Closed dddddgz closed 6 months ago

dddddgz commented 1 year ago

Desktop (please complete the following information):

ttkbootstrap Version: 1.10.1 Python Version: Python 3.9.9 OS: Windows 10 22H2 Home Edition 19045.2965 This is what pip shows:

C:\Users\bruce>pip show ttkbootstrap
Name: ttkbootstrap
Version: 1.10.1
Summary: A supercharged theme extension for tkinter that enables on-demand modern flat style themes inspired by Bootstrap.
Home-page: https://github.com/israel-dryer/ttkbootstrap
Author: Israel Dryer
Author-email: israel.dryer@gmail.com
License:
Location: d:\python399\lib\site-packages
Requires: pillow
Required-by:

Describe the bug

Below is my code:

from ttkbootstrap import *
window = Window("title for bug report")
button = Button(window, font=("Microsoft Yahei UI", 20))
button.pack()
window.mainloop()

Error messages:

PS E:\Python123> python test.py
Traceback (most recent call last):
  File "E:\Python123\test.py", line 3, in <module>
    button = Button(window, font=("Microsoft Yahei UI", 20))
  File "D:\Python399\lib\site-packages\ttkbootstrap\style.py", line 4941, in __init__
    func(self, *args, **kwargs)
  File "D:\Python399\lib\tkinter\ttk.py", line 607, in __init__
    Widget.__init__(self, master, "ttk::button", kw)
  File "D:\Python399\lib\tkinter\ttk.py", line 552, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "D:\Python399\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
_tkinter.TclError: unknown option "-font"

To Reproduce

  1. Create file test.py.
  2. Paste following code:
    from ttkbootstrap import *
    window = Window("title for bug report")
    button = Button(window, font=("Microsoft Yahei UI", 20))
    button.pack()
    window.mainloop()
  3. Run this program.
  4. See error.

Expected behavior

I want to achieve effect that can change the font to microsoft yahei ui, and change the font size to 20. But it raised an error message, it shows attribute font is unknown option.

Screenshots

The following is a screenshot of error: Snipaste_2023-07-13_15-03-54

Additional context

This is weird because the font attribute works just fine on tkinter and other ttkbootstrap controls (e.g. Label).

In addition, I tried to use tkinter.Button instead, but tkinter does not support bootstyle.

Thanks!

asorge29 commented 8 months ago

Yes, please fix this!!

Haugor commented 6 months ago

Yes, I want to change the font size of Button, but it show the same error.

dddddgz commented 6 months ago

Does anyone know how to fix this? Otherwise I'd have to use the tkinter GUI.

rdbende commented 6 months ago

I don't know where the thought came from that you can change the font of a themed button, because the idea is that you don't (manual page for reference: https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_button.html). The Label widget is the only exception.

Instead you can configure the style class that is applied to the button. Like this:

from ttkbootstrap import *
window = Window("title for bug report")

Style().configure("TButton", font=("Microsoft Yahei UI", 20))

button = Button(window, text="Spam ham egg")
button.pack()
window.mainloop()
dddddgz commented 6 months ago

I roughly understand what you mean. Does it mean that only Label can set the font parameter straightly?

rdbende commented 6 months ago

Yes. (Aand plain, non-themed tkinter widgets too, but that's irrelevant).

dddddgz commented 6 months ago

Thanks for the quick reply.