将 Python 3 tkinter 和 ttk 代码反向移植到 Python 2.7

Backporting Python 3 tkinter & ttk code to Python 2.7

我正在学习 Sentdex 教程,found here, specifically this part 关于在 Python 中创建 GUI。但是教程在 python 3 中,我使用的是 2.7.

导入Tkinter没问题,但是当我导入ttk然后继承到class时,问题出现了。在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()

问题似乎是当 classes 继承 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()

如果您使用ttk.Button,它绝对使用ttk按钮。它不可能做任何其他事情,因为您明确表示要使用 ttk 模块中的 Button class。

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

除了导入的方式,python 2.x 和 3.x.

中的 tkinter 几乎没有区别