Reputation: 73
Here is my code
from Tkinter import *
import ttk, tkMessageBox
import os
font = ("Avenir", 24)
b = ttk.Style()
b.configure('TButton', font=font)
class LoginScreen(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side=TOP, fill=BOTH, expand=True)
self.frames = {}
for F in (Login, Register):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(Login)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Login(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="screen 1")
button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register))
button.pack()
label.pack()
class Register(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="screen 2")
label.pack()
if __name__ == '__main__':
app = LoginScreen()
app.title("Login")
app.mainloop()
When I run this, I get this screen: Working Screen without ttk
But as soon as I change:
button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register))
to
button = ttk.Button(self, text="move", style='TButton', command=lambda: controller.show_frame(Register))
A Secondary Window is opened and the font doesn't change.
I'm hoping there is something simple I am overlooking, but this method of styling ttk widgets is the only way I've seen it done online.
I don't want the window, and as I have stated before it seems to magically appear when I apply the 'b' style to a button.
Thanks for reading.
Upvotes: 7
Views: 2524
Reputation: 13729
The secondary window is caused by your line 7. When you call ttk.Style
it needs a root window to work with, and if one has not been created yet it creates one. To fix this you need to move lines 7 and 8 to a point after creating the root window (calling Tk()
).
if __name__ == '__main__':
app = LoginScreen()
app.title("Login")
b = ttk.Style()
b.configure('TButton', font=font)
app.mainloop()
Upvotes: 9