简体   繁体   English

什么。? (点感叹号)在python中是什么意思?

[英]What does .! (dot exclamation mark) mean in python?

I wrote the following program:我写了以下程序:

from tkinter import *

root = Tk()

temp = None
def changeColor(event, e):
    # print('Positional arumgent passed:', x)
    print('event: ', event.widget)
    e2 = event.widget
    if e2['bg'] == 'white':
        e2['bg'] = 'black'
    elif e2['bg'] == 'black':
        e2['bg'] = 'white'
    global temp
    temp = event

entries = [[None for i in range(3)] for j in range(3)]

for y in range(3):
    for x in range(3):
        e = Entry(root, width=3, bg='white', bd=0, borderwidth=3)
        e.bind('<Double-Button-1>', lambda x: changeColor(x, e))
        e.grid(column=x, row=y)
        entries[y][x] = e

root.mainloop()

which, in the terminal, produced the output,其中,在终端中,生产了 output,

event:  .!entry5
event:  .!entry6
event:  .!entry8
...

When I examined the one of the event.widgets (stored in temp) in the terminal, I got,当我检查终端中的 event.widgets (存储在 temp 中)之一时,我得到了,

>>> temp.widget
<tkinter.Entry object .!entry8>
>>> print(temp.widget)
.!entry8

I've never seen this syntax before and I was unable to find anything in the docs or here on stackoverflow about it.我以前从未见过这种语法,也无法在文档或 stackoverflow 上找到任何关于它的内容。

Does anyone know what it is?有谁知道它是什么? Could you kindly explain/describe?你能解释/描述一下吗?

.! is not a general Python thing, it's just how the tkinter library names widgets that are not otherwise given a name.不是一般的 Python 的东西,它只是 tkinter 库如何命名没有以其他方式命名的小部件。 entry comes from here , since this widget is an Entry class. entry来自这里,因为这个小部件是一个Entry class。 The exclamation point & number suffix are assigned here , presumably being given a ! 这里分配了感叹号和数字后缀,大概是给了一个! so that auto-generated, class-name-derived widget names don't overlap with those assigned by the user.这样自动生成的、类名派生的小部件名称就不会与用户指定的名称重叠。 And the leading period is how widgets are named when they don't have a named parent;领先时期是小部件在没有命名父级时如何命名; if they did, it would be <parentname>.<childname> .如果他们这样做了,那将是<parentname>.<childname>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM