【发布时间】:2013-11-30 16:51:13
【问题描述】:
我目前正在使用 lambda 使 tkinter 按钮依次执行两件事:
def classManip():
cManip = tk.Toplevel()
cManip.title('Class Manipulator')
cManip.minsize(400,100)
cManip.maxsize(400,100)
databaseEntry = ttk.Entry(cManip, width = 25)
databaseEntry.place(relx = .5, rely = .375, anchor = "c")
entrySubmit = ttk.Button(cManip, text = "Enter", width = 20, command = lambda : connectDatabase(databaseEntry.get()) & cManip.destroy())
entrySubmit.place(relx = .5, rely = .625, anchor="c")
cManip.mainloop()
这是我主要代码中的函数;我的主 tkinter 窗口上有一个按钮,该按钮具有运行此功能的命令。 连接 databaseEntry 函数是在名为 scripts 的文件夹中形成另一个名为 databaseManip 的文件,我使用以下方法导入:
from scripts.databaseManip import connectDatabase
该文件中的代码是:
import sqlite3, sys, os
import tkinter as tk
from win32api import GetSystemMetrics
#connects or creates database
def connectDatabase(name):
name = str(name)
screenWidth = GetSystemMetrics (0)
screenHeight = GetSystemMetrics (1)
if os.path.isfile("classDbFiles/" + name + ".db"):
conn = sqlite3.connect("classDbFiles/" + name + ".db")
tk.messagebox.showinfo(message="Connected to %s successfully" % (str(name + ".db")), title = "File Found")
else:
conn = sqlite3.connect("classDbFiles/" + name + ".db")
tk.messagebox.showinfo(message = "The database file %s was created and opened successfully" % (str(name + ".db")), title = "Success")
我想让程序做的是运行创建或打开 .db 文件的数据库函数,然后关闭 tkinter 窗口,有趣的是它确实有效,但它返回错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "E:\Program Files\Python\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Users\Patrick\Dropbox\Computing Project\mainApp.py", line 52, in <lambda>
entrySubmit = ttk.Button(cManip, text = "Enter", width = 20, command = lambda : connectDatabase(databaseEntry.get()) & cManip.destroy())
TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'
我一直在寻找答案,但似乎没有什么适合 lambda,所以我迷路了。代码有什么问题?
【问题讨论】:
-
因为 connectDatabase 没有返回任何东西,所以你隐含地得到
None,而销毁返回None...并且因为None & None没有意义...你得到那个错误...您会/希望会发生什么? -
您期望
connectDatabase和cManip.destroy返回什么?