本文介绍了tkk.Frame 中奇怪的背景外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tkinter 中发现了一个异常.我在下面附上了我的脚本以显示异常.

I discovered an anomaly in tkinter. I have attached my script below to show the anomaly.

运行这个脚本,我发现在我的系统 (Ubuntu 16.04.03) 上 ttk.Frame 的背景看起来很奇怪.如果我将鼠标指针移入和移出 ttk.Frame,背景外观可能会发生变化并出现乱码(例如可能会出现来自其他背景窗口的背景,可能会出现奇怪的颜色图案,....).

Running this script, I found on my system (Ubuntu 16.04.03) that the background of the ttk.Frame appears strange. If I move my mouse pointer in and out of the ttk.Frame, the background appearance can change and appear gibberish (e.g. backgrounds from other background windows may appear, strange color patterns may appear,....).

经过一些调查,我发现通过将 background='light Purple' 替换为 background='white',异常消失并且 ttk.Frame 外观的行为与定义的一样.也就是说,错误是由于使用了 tkinter 无法识别的背景/颜色名称引起的.

After some investigations, I discovered that by replacing background='light purple' with background='white', the anomaly disappears and the ttk.Frame appearance behaves as was defined. That is, the fault was caused by using a background/colorname that tkinter did not recognised.

问题:

  1. 此异常是否发生在 Mac、Windows 或其他 Linux 系统中?

  1. Does this anomaly occur in a Mac, Windows or other Linux systems?

tkinter 接受的有效背景名称是什么?我不是能够在 tkinter 的 __init__.pyttk.py 中找到它.哪里可以我找到了吗?

What are the valid background names that tkinter accepts? I was notable to locate it in tkinter's __init__.py or ttk.py. Where canI find them?

测试脚本:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import tkinter as tk
import tkinter.ttk as ttk


class App(ttk.Frame):

    def __init__(self, parent=None, *args, **kwargs):

        style=ttk.Style()
        style.configure('App.TFrame', background='light purple')

        ttk.Frame.__init__(self, parent, style='App.TFrame', borderwidth=10,
                           relief='raised', width=200, height=200)
        self.parent = parent
        self.parent.geometry('300x300')


if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    app.grid(row=0, column=0, sticky='nsew')
    app.grid_propagate(0)

    root.mainloop()

推荐答案

在 Win 10, 3.7.0b1 上,没有给定背景的结果是白色,识别的背景就是它,并且背景未知(浅紫色"或'xyz') 是黑色的.至于公认的颜色名称:

On Win 10, 3.7.0b1, no background given results in white, a recognized background is what it is, and an unknown background ('light purple' or 'xyz') is black. As for recognized color names:

NMT 8.5 参考说颜色 'white'、'black'、'red'、'green'、'blue'、'cyan'、'yellow' 和 'magenta' 将始终可用.其他名称可能有效,具体取决于您的本地安装.".

The NMT 8.5 reference says "The colors 'white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', and 'magenta' will always be available. Other names may work, depending on your local installation.".

tcl/tk 8.6 文档列出了数百个颜色名称(但不是浅紫色"!)以及一些特定于 MacOS 和 Windows 的名称.

The tcl/tk 8.6 docs list hundreds of color names (but not 'light purple'!) plus some specific to MacOS and Windows.

这篇关于tkk.Frame 中奇怪的背景外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:31