向后移植 Python 3 tkinter &ttk 代码到 Python 2.7 [英] Backporting Python 3 tkinter & ttk code to Python 2.7

查看:77
本文介绍了向后移植 Python 3 tkinter &ttk 代码到 Python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Sentdex 教程,在这里找到,特别是这部分 关于在 Python 中创建 GUI.但是教程是在 python 3 中,我使用的是 2.7.

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

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

Importing Tkinter is fine, however when I come to importing ttk and then inheriting it into the class, a problem arises. In python 2.7 ttk is a separate module, which means is not in the Tkinter module.

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 时,当我这样做时:

The problem appears to be when the classes inherit Tk, when I do:

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

它不使用ttk,按钮看起来仍然相同(就像使用tk.Button 时一样).我怎样才能让按钮看起来像 ttk 按钮.

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

完整代码:

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 类.

If you use ttk.Button, it absolutely will use the ttk button. It can't possibly do anything else because you're explicitly saying to use the Button class from the ttk module.

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

Note: Depending on what platform you're on, the tk and ttk buttons may look identical.

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

Other than the way you do the imports, there's virtually no difference between tkinter in python 2.x and 3.x.

这篇关于向后移植 Python 3 tkinter &ttk 代码到 Python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆