没有 * 的 Tkinter 导入? [英] Tkinter importing without *?

查看:27
本文介绍了没有 * 的 Tkinter 导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我过去的编程中,我使用了以下代码:

In my past programming i used the following code:

from tkinter import *
Gui = Tk()

但有人告诉我,由于很多原因,导入 * 并不好,但现在当我想导入时

but someone told me that importing * was not good for many reasons but now when i want to import

from tkinter import geometry

它说几何不是模块的东西(名称).

it says geometry not a module thing (name).

当我这样做时:

    import tkinter 
tkinter.geometry(500x500)

它说模块"对象没有属性几何"

it says 'module' object has no attribute 'geometry'

有人可以解释我如何使用 tkinter 以各种不同的方式导入吗?不仅适用于几何,而且适用于大多数 tkinter 模块...???

Can someone explain me how to import with tkinter all the different ways?? Not only for geometry but most of the tkinter modules...???

推荐答案

那是因为 module tkinter 没有 geometry 函数.这是Tk 实例.

That's because the module tkinter doesn't have a geometry function. It's the Tk instances that do.

这是完成您想要做的事情的好方法:

Here's a good way to accomplish what you're trying to do:

import tkinter as tk     # tk is a convenient, easy to type alias to use for tkinter.
gui = tk.Tk()
gui.geometry("500x500")   # don't forget the quotes

为什么 from tkinter import * 不是导入 tkinter 的理想方式

顺便说一句,谁告诉你 from tkinter import * 是一个坏主意是正确的 - 当你这样做时,你将所有 tkinter 的命名空间加载到模块的命名空间中.

Why from tkinter import * is a non-ideal way to import tkinter

As an aside, whoever told you that from tkinter import * was a bad idea was correct - when you do that, you load all of tkinter's namespace into your module's namespace.

如果这样做,最终可能会导致令人不快的命名空间冲突,如下所示:

If you do that, you can end up with unpleasant namespace collisions, like this:

from tkinter import *
gui = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)

# Traceback (most recent call last):
#   File "stackoverflow.py", line 98, in <module>
#     Label1 = Label(gui, text=Label)
# TypeError: 'str' object is not callable

您已经覆盖了对 tkinter 的 Label 小部件的引用 - 因此您无法再创建任何标签!当然,无论如何您都不应该像这样将局部变量大写,但是当您可以这样做时,为什么还要担心避免那些命名空间问题:

You've overwritten the reference to tkinter's Label widget - so you can't create any more Labels! Of course you shouldn't be capitalizing local variables like that anyways, but why worry about avoiding those namespace problems when you can do this instead:

import tkinter as tk

这个 ^^^^ 导入方法也是更可取的,因为如果在某个时候你想将 Tkinter 换成另一个具有类似实现的模块,而不是为 Tkinter 模块的所有元素梳理你的代码,你可以去像这样:

This ^^^^ import method is also preferable because if at some point you want to swap Tkinter out for another module with a similar implementation, instead of combing through your code for all elements of the Tkinter module, you can just go like this:

#import tkinter as tk
import newTkinter as tk

你已经准备好了.或者,如果你想使用另一个模块,它的类和方法碰巧有一些相同的名称,以下会造成混乱:

And you're all set. Or, if you want to use another module that happens to have some of the same names for its classes and methods, the following would cause chaos:

from tkinter import *
from evilOverlappingModule import *

不过这样就好了:

import tkinter as tk
import evilOverlappingModule as evil

这篇关于没有 * 的 Tkinter 导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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