0

I have a app which uses ttkbootstrap and as a learning exercise wanted to turn it into a class based app. I am falling at the first hurdle so to speak as the basic window shows up as a different size.The first example is fine.

import tkinter as tk
from tkinter import *

import ttkbootstrap as tb 

#root = tk.Tk()
root=tb.Window(themename="terry")

root.title("Stableford League")
root.geometry("1000x1000")


root.mainloop()

However the following produces a larger window:

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1000x1000")


if __name__ == "__main__":
    app = App()
    app.mainloop()

My screen resolution is 1920 x 1080 and scale is set to 150% but I cannot understand why the displayed windows are different sizes.

following your advice I arrive at the following code

import tkinter as tk
from tkinter import ttk
import  ttkbootstrap as tb
from ttkbootstrap import Style

class App(tb.Window):
    def __init__(self):
        super().__init__()
        self.title("Stableford League")
        self.geometry("1000x1000")
        self.style = Style(theme="terry")


if __name__ == "__main__":
    app = App()
    app.mainloop()

The line self.style = Style(theme = "terry") worked before inheriting from tb.Window but now throws an error.

9
  • 2
    The code isn't identical, so why do you expect to be behave identically? One uses ttkbootstrap, one doesn't. Also, is "terry" a custom theme? I get an error that it's not a valid theme. Commented Mar 11 at 17:30
  • For consistency, your App class should inherit from tb.Window, making it equivalent to root in your first example. And @BryanOakley: given that OP's (user)name is Terry, I assume it is indeed a custom theme. Commented Mar 11 at 17:55
  • @ Bryan Oakley sorry "terry" is a custom theme changing to one of the standard themes will not affect this issue. Commented Mar 11 at 19:30
  • @ JRiggles I couldn't see how to inherit from ttkbootstrap. I did insert the line : "self.style = Style(theme="terry") which changed the colour but had no effect on window size. Commented Mar 11 at 19:35
  • It is because ttkbootstrap handles high DPI (i.e. DPI awareness), but tkinter does not. To inherit from ttkbootstrap, change class App(tk.Tk) to class App(tb.Window). Commented Mar 12 at 1:29

1 Answer 1

1

The difference in window size is because ttkbootstrap is DPI awareness, but tkinter is not.

To apply certain theme when using class, you can either setting themename option when initializing the class object:

class App(tb.Window):
    def __init__(self):
        super().__init__(themename='terry')
        ...

or set it using theme_use() of Style class:

class App(tb.Window):
    def __init__(self):
        super().__init__()
        self.style.theme_use('terry')
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.