2017-03-04 90 views
0

在這裏打開二級窗口是我的代碼TTK應用樣式

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() 

當我運行它,我得到這個畫面: Working Screen without ttk

但只要我改變:

button = Button(self, text="move", font=font, command=lambda: controller.show_frame(Register)) 

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.

我希望有一些簡單的我可以忽略,但是這種ttk構件樣式的方法是我看到它在線完成的唯一方法。

我不想要這個窗口,正如我之前所說的,當我將'b'樣式應用到按鈕時,它似乎奇蹟般地出現。

感謝您的閱讀。

回答

0

輔助窗口是由行7引起的。當您調用ttk.Style時,它需要一個根窗口來處理,如果還沒有創建它,它會創建一個。要解決這個問題,需要在創建根窗口(調用Tk())後將第7行和第8行移動到某一點。

if __name__ == '__main__': 
    app = LoginScreen() 
    app.title("Login") 
    b = ttk.Style() 
    b.configure('TButton', font=font) 
    app.mainloop() 
+0

很好用!非常感謝! –

+0

@MaxO隨時。順便說一下,堆棧溢出表示感謝的方式是通過upvoting並接受答案。 http://stackoverflow.com/help/someone-answers – Novel