![](/img/trans.png)
[英]Unsupported operand type(s) for *: 'NoneType' and 'NoneType' (Python)
[英]Python: Unsupported operand type(s) for &: 'NoneType' and 'NoneType' whilst using lambda
我目前正在使用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主窗口上有一個按鈕,該按鈕具有運行此功能的命令。 connect 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似乎什么也沒有,所以我迷路了。 代碼到底是什么?
&
不會按照您的想法去做。 它找到兩個對象的按位and
。 而是嘗試定義函數:
def function():
connectDatabase(databaseEntry.get())
cManip.destroy()
entrySubmit = ttk.Button(cManip, text="Enter", width=20, command=function)
您也可以將&
替換為and
,如果第一個函數調用始終僅返回None
(或另一個錯誤值), 則可以使用& , 但這是一種討厭、,瑣,聰明且難以理解的方式來做您想要的事情並可能導致您和任何閱讀您的代碼的人感到困惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.