[英]Tkinter method on returning value from TopLevel object to root object
I created a Tkinter app that calls TopLevel
objects from another file (for menu bar options).我创建了一个 Tkinter 应用程序,它从另一个文件调用
TopLevel
对象(用于菜单栏选项)。
In my main.py
, I have this code:在我的
main.py
中,我有以下代码:
...
def create():
funct.createNew()
...
menubar = tk.Menu(window)
#file menu
file = tk.Menu(menubar, tearoff=0)
file.add_command(label='Create new', command=create)
...
And the function being called from functions.py
file (I removed the window attributes for simplicity):并且从
functions.py
文件中调用的函数(为简单起见,我删除了窗口属性):
def linkToDB():
global create
#call the function to create virtual file
file = sql.generateDB()
#destroy the createNew window
create.destroy()
#debugging purposes
print("Virtual file:", file)
def createNew():
#make this global to destroy this window later
global create
create = tk.Toplevel()
...
#option 1
container1 = tk.Frame(create)
...
#generate virtual SQLite file
btn1 = tk.Button(container1, text="Create", command= lambda: linkToDB())
The above code displays a TopLevel
window that will be called by Create button in main.py
, which in turn calls a function from sql.py
(by that button within TopLevel
window) to create temporary SQlite file:上面的代码显示了一个
TopLevel
窗口,该窗口将由main.py
中的Create按钮调用,然后调用sql.py
中的一个函数(通过TopLevel
窗口中的那个按钮)来创建临时 SQlite 文件:
import sqlite3 as sq
#create a virtual sqlite file
def generateDB():
temp = sq.connect(':memory:')
temp.execute...
return temp
My problem is how to return the value of temp
from generateDB()
to main.py
(specifically after destroying the TopLevel
window)?我的问题是如何将
temp
的值从generateDB()
返回到main.py
(特别是在破坏TopLevel
窗口之后)? I am confused on how to pass this value across the .py files.我对如何在 .py 文件中传递这个值感到困惑。 And whether my approach is viable or not (also looking for suggestions).
以及我的方法是否可行(也在寻找建议)。
PS I intentionally destroyed the TopLevel
window in linkToDB()
since it is guaranteed that my temporary SQlite file will be generated. PS 我故意破坏了
linkToDB()
中的TopLevel
窗口,因为它保证会生成我的临时 SQlite 文件。
You can modify funct.createNew()
to return the virtual file to main.py
.您可以修改
funct.createNew()
以将虚拟文件返回到main.py
。 However you need to make the toplevel window a modal window in order to return the virtual file after the toplevel window is destroyed.但是,您需要将顶层窗口设置为模态窗口,以便在顶层窗口被销毁后返回虚拟文件。
Below is the modified functions.py
:以下是修改后的
functions.py
:
import tkinter as tk
import sql
# pass the toplevel window as an argument instead of using global variable
def linkToDB(create):
# use an attribute of the toplevel window to store the "virtual db file"
create.file = sql.generateDB()
create.destroy()
print('Virtual file:', create.file)
def createNew():
create = tk.Toplevel()
...
container1 = tk.Frame(create)
...
btn1 = tk.Button(container1, text='Create', command=lambda: linkToDB(create))
...
# wait for window destroy (works like a modal dialog)
create.wait_window(create)
# return the "virtual db file"
return create.file
Then you can get the virtual db file in main.py
:然后您可以在
main.py
中获取虚拟数据库文件:
def create():
# if you want the "virtual db file" be accessed by other function
# declare it as global variable
#global db_file
db_file = funct.createNew()
print(db_file)
...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.