有关 Python 3 的 tkinter 和 ttk 的最新教程

发布于 2024-11-26 15:38:49 字数 1536 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初相遇 2024-12-03 15:38:49

我发现 TkDocs 教程非常有用。它描述了使用 Python 和 Tkinterttk 构建 Tk 接口,并记录了 Python 2 和 3 之间的差异。它还有 Perl 示例, Ruby 和 Tcl,因为目标是教授 Tk 本身,而不是特定语言的绑定。

我没有从头到尾经历整个事情,而是只使用了一些主题作为我所坚持的事情的例子,但它很有指导意义,而且写得很舒服。今天阅读简介和前几节让我想我将开始完成其余部分。

最后,它是最新的,并且该网站的外观非常漂亮。他还有许多其他值得查看的页面(小部件、资源、博客)。这家伙做了很多事情,不仅是为了教导 Tk,而且是为了提高人们的认识,让人们认识到它不再是以前那样丑陋的野兽了。

I have found the TkDocs tutorial to be very useful. It describes building Tk interfaces using Python and Tkinter and ttk and makes notes about differences between Python 2 and 3. It also has examples in Perl, Ruby and Tcl, since the goal is to teach Tk itself, not the bindings for a particular language.

I haven't gone through the whole thing from start to finish, rather have only used a number of topics as examples for things I was stuck on, but it is very instructional and comfortably written. Today reading the intro and first few sections makes me think I will start working through the rest of it.

Finally, it's current and the site has a very nice look. He also has a bunch of other pages which are worth checking out (Widgets, Resources, Blog). This guy's doing a lot to not only teach Tk, but also to improve people's understanding that it's not the ugly beast that it once was.

飞烟轻若梦 2024-12-03 15:38:49

我推荐 NMT Tkinter 8.5 参考

一些示例中使用的模块名称是Python 2.7中使用的模块名称。
以下是 Python 3 中名称更改的参考:链接

ttk 是你可以选择一个预先存在的 主题,
这是一整套 样式应用于ttk 小部件。

这是我编写的一个示例(针对 Python 3),它允许您从组合框中选择任何可用主题:

import random
import tkinter
from tkinter import ttk
from tkinter import messagebox

class App(object):

    def __init__(self):
        self.root = tkinter.Tk()
        self.style = ttk.Style()
        available_themes = self.style.theme_names()
        random_theme = random.choice(available_themes)
        self.style.theme_use(random_theme)
        self.root.title(random_theme)

        frm = ttk.Frame(self.root)
        frm.pack(expand=True, fill='both')
    # create a Combobox with themes to choose from
        self.combo = ttk.Combobox(frm, values=available_themes)
        self.combo.pack(padx=32, pady=8)
    # make the Enter key change the style
        self.combo.bind('<Return>', self.change_style)
    # make a Button to change the style
        button = ttk.Button(frm, text='OK')
        button['command'] = self.change_style
        button.pack(pady=8)

    def change_style(self, event=None):
        """set the Style to the content of the Combobox"""
        content = self.combo.get()
        try:
            self.style.theme_use(content)
        except tkinter.TclError as err:
            messagebox.showerror('Error', err)
        else:
            self.root.title(content)

app = App()
app.root.mainloop()

旁注:我注意到使用 Python 3.3 时有一个“vista”主题可用(但不是 2.7)。

I recommend the NMT Tkinter 8.5 reference.

The module names used in some examples are those used in Python 2.7.
Here's a reference for the name changes in Python 3: link

One of the conveniences of ttk is that you can choose a preexisting theme,
which is a full set of Styles applied to the ttk widgets.

Here's an example I wrote (for Python 3) that allows you to select any available theme from a Combobox:

import random
import tkinter
from tkinter import ttk
from tkinter import messagebox

class App(object):

    def __init__(self):
        self.root = tkinter.Tk()
        self.style = ttk.Style()
        available_themes = self.style.theme_names()
        random_theme = random.choice(available_themes)
        self.style.theme_use(random_theme)
        self.root.title(random_theme)

        frm = ttk.Frame(self.root)
        frm.pack(expand=True, fill='both')
    # create a Combobox with themes to choose from
        self.combo = ttk.Combobox(frm, values=available_themes)
        self.combo.pack(padx=32, pady=8)
    # make the Enter key change the style
        self.combo.bind('<Return>', self.change_style)
    # make a Button to change the style
        button = ttk.Button(frm, text='OK')
        button['command'] = self.change_style
        button.pack(pady=8)

    def change_style(self, event=None):
        """set the Style to the content of the Combobox"""
        content = self.combo.get()
        try:
            self.style.theme_use(content)
        except tkinter.TclError as err:
            messagebox.showerror('Error', err)
        else:
            self.root.title(content)

app = App()
app.root.mainloop()

Side note: I've noticed that there is a 'vista' theme available when using Python 3.3 (but not 2.7).

思慕 2024-12-03 15:38:49

我建议阅读文档。它简单且权威,适合初学者。

I recommend reading the documentation. It is simple and authoritative, and good for beginners.

递刀给你 2024-12-03 15:38:49

它并不是很新鲜,但是 this 很简洁,而且从我所看到的情况来看,它对于 Python 2 和 3 都是有效的。

It's not really fresh but this is concise, and from what I've seen valid either for Python 2 and 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文