Tkinter and ttk in python 2.7

I am following the Sentdex tutorial found here , in particular this part about creating a GUI in Python. However, the tutorial is in Python 3, and I'm using 2.7.

Importing Tkinter is good, however, when I ttk to import ttk and then inherit it in the class, a problem arises. In python 2.7, ttk is a separate module, which means that it is not part of the Tkinter class.

 import Tkinter as tk import ttk LARGE_FONT= ("Verdana", 12) class SeaOfBTCApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) tk.Tk.wm_title(self, "Seal of BTC") container = tk.Frame(self) container.pack(side="top", fill="both", expand = True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (StartPage, PageOne, PageTwo): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(StartPage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() 

The problem seems to be when classes inherit Tk when I do:

 button = ttk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne)) 

It doesn't use ttk , the buttons still look the same (like when it was tk.Button ). How to make the buttons look like ttk buttons, please help.

Full code:

 import Tkinter as tk from ttk import * LARGE_FONT= ("Verdana", 12) class SeaOfBTC(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) tk.Tk.wm_title(self, "Sea of BTC") container = tk.Frame(self) container.pack(side="top", fill="both", expand = True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (StartPage, PageOne, PageTwo): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(StartPage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() class StartPage(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self,parent) label = tk.Label(self, text="Start Page", font=LARGE_FONT) label.pack(pady=10,padx=10) button = ttk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne)) button.pack() button2 = ttk.Button(self, text="Visit Page 2", command=lambda: controller.show_frame(PageTwo)) button2.pack() class PageOne(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) label = tk.Label(self, text="Page One!!!", font=LARGE_FONT) label.pack(pady=10,padx=10) button1 = ttk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage)) button1.pack() button2 = ttk.Button(self, text="Page Two", command=lambda: controller.show_frame(PageTwo)) button2.pack() class PageTwo(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT) label.pack(pady=10,padx=10) button1 = ttk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage)) button1.pack() button2 = ttk.Button(self, text="Page One", command=lambda: controller.show_frame(PageOne)) button2.pack() app = SeaOfBTC() app.mainloop() 
+1
user-interface tkinter ttk
Apr 04 '15 at 9:20
source share
1 answer

If you use ttk.Button , it will absolutely use the ttk button. It cannot do anything because you explicitly say that you are using the Button class from the ttk module.

Note. Depending on which platform you are on, the tk and ttk buttons may look the same.

Besides the fact that you are importing, there is practically no difference between tkinter in python 2.x and 3.x.

+1
Apr 04 '15 at 12:26
source share



All Articles