关于python:在tkinter中声明全局并导入模块
declaration of global in tkinter with import of module
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from tkinter import *
from tkinter import ttk import calculation_feet_metres from calculation_feet_metres import *
root = Tk() ''' value = float(feet.get()) mainframe = ttk.Frame(root, padding="3 3 12 12") feet_entry = ttk.Entry(mainframe, width = 7, textvariable = feet)
ttk.Label(mainframe, textvariable = metres).grid(column=2, row=2, sticky=(W,E)) ttk.Label(mainframe, text ='feet').grid(column=3, row=1, sticky=W) for child in mainframe.winfo_children(): feet_entry.focus() root.mainloop() |
以上返回
|
1
2 |
value = float(feet.get())
NameError: global name 'feet' is not defined. |
如果我"激活"函数
我试图了解如何导入一个模块(在这种情况下,我还有一个包含相同
从不同模块导入的函数在它们被导入的模块中看不到全局变量。
全局变量对于定义函数的模块是"本地的"。
你可以将你的函数package在一个 lambda 中:
|
1
|
ttk.Button(mainframe, text="Calculate", command = lambda: calculate(feet)).grid(column=3, row=3, sticky=W)
|
并更改您的
|
1
|
def calculate(feet):
|
相关讨论
- 有了这个,我得到一个错误,我正在向 lambda 提供 0 个参数。我担心我对此很陌生,所以我真的不明白我只是在复制你写的东西,所以也许你假设了一些我应该知道的东西???使这项工作
-
@user1478335:对不起,我的错误,按钮
command 参数不带任何参数。我删除了a 参数。