【发布时间】:2017-06-04 15:37:38
【问题描述】:
我有 3 个 python 文件:login、teacher_ui 和 student_ui。这三个都使用 tkinter。登录文件将名称作为输入,如果该名称是数据库中的有效表,则登录文件会导入 student_ui 文件来运行它。
我遇到的问题是 student_ui 文件需要一个名为name 的变量,它是login 中的输入。我正在努力将变量导入student_ui,因为它一直在变化。
我在login 中加载student_ui 文件的代码是:
elif name_data in names_list:
opening_window.destroy()
import student_ui
然后运行student_ui,它提供了一个不同的接口。 name_data的代码是:name_data = name.get().lower()
student_ui 中需要name_data 的代码行是:user_table_name = name_data。此行抛出 NameError,因为未定义 name。
因此,当login 加载student_ui 时,我如何让student_ui 从login 中获取name_data?
student_ui 的部分代码是:
number_words = {
"Forty Five" : 45,
...
"Nine Thousand, Eight Hundred and Sixty Four" : 9864
}
user_table_name = name_data
query = 'SELECT _45 FROM {} ORDER BY runid DESC LIMIT
3'.format(user_table_name)
c.execute(query)
status_1 = c.fetchall()
if ('true',) in status_1:
status_1 = True
else:
status_1 = False
还有用于标签、输入、标记和大量数据库写入和读取的代码。
【问题讨论】:
-
你的程序结构如何,运行不同的文件是什么?
-
login文件运行格式不同的文件:if condition: import student_uielif other condition: import teacher_ui。 -
UI 是用类等构建的还是只是代码?
-
我会创建一个类并从模块中导入该类。在构造函数中,它将接收名称变量
-
回复:@IsaacDj 建议:这将取决于代码的作用。其中一些可能必须移动到类的方法中——比如它的
__init__()方法或您需要创建(并在某处调用)的新方法。
标签: python python-3.x variables python-import