Tkinter和ttk在python 2.7中

我正在关注在Python中创建GUI的Sentdex教程found herespecifically this part . 但是教程是在python 3中,我使用的是2.7 .

导入 Tkinter 很好,但是当我导入 ttk 然后将其继承到类中时会出现问题 . 在python 2.7中 ttk 是一个单独的模块,意味着不在 Tkinter 类中 .

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

问题似乎是当类继承Tk时,我这样做:

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

它不使用 ttk ,按钮看起来仍然相同(就像 tk.Button 时那样) . 如何使按钮看起来像ttk按钮,请帮忙 .

完整代码:

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)

2 years ago

如果你使用 ttk.Button ,它绝对会使用ttk按钮 . 它可以't possibly do anything else because you'明确地说要使用 ttk 模块中的 Button 类 .

注意:根据您所使用的平台,tk和ttk按钮可能看起来完全相同 .

除了你进行导入的方式之外,python 2.x和3.x中的tkinter几乎没有区别 .