我正在嘗試提取 TextBox 內容并將其插入 SQLite 資料庫。我正在使用該guizero
模塊。
我寫了這個 python測驗腳本
from guizero import App, PushButton,TextBox, Text,Box
import sqlite3
global x
global name
def insertVaribleIntoTable(x):
try:
sqliteConnection = sqlite3.connect('DB')
cursor = sqliteConnection.cursor()
cursor.execute('''INSERT INTO employee VALUES (?)''',(x,))
sqliteConnection.commit()
cursor.execute('''SELECT emp_name FROM employee''')
print("database content : \n")
print(cursor.fetchall())
cursor.close()
except sqlite3.Error as error:
print("Failed to insert Python variable into sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
def when_clicked():
print(name)
strname = str(name)
print(strname)
insertVaribleIntoTable(strname)
name.clear()
app = App(title="Registration")
message = Text(app, text="Registration")
entrybox = Box(app, width=185, height='fill')
Text(entrybox, text='Name ', align='left')
name = TextBox(entrybox, align='left', width=185)
button = PushButton(app, text="Add", command=when_clicked)
app.display()
現在,在 TexyBox 中,當我插入userAA并單擊 Add 按鈕時,我得到了這個輸出
database content :
[('User1',), ('User2',), ('User3',), ("[TextBox] object with text 'userAA'",)]
雖然預期的輸出是
database content :
[('User1',), ('User2',), ('User3',), ('userAA',)]
有人知道如何解決這個問題嗎?
uj5u.com熱心網友回復:
看來您的name
變數有問題。它是一個TextBox
類的實體,通過 chain: 繼承其__str__
方法Component -> Widget -> TextWidget -> TextBox
。
它看起來像這樣:
def __str__(self):
return self.description
課內TextBox
:
@property
def description(self):
"""
Returns the description for the widget.
"""
return "[TextBox] object with text '{}'".format(self.value)
你可以在這里探索它:https ://github.com/lawsie/guizero/tree/master/guizero
所以我認為你應該使用這樣的東西:
def when_clicked():
print(name)
strname = name.value # str(name) will call name.descirption
print(strname)
insertVaribleIntoTable(strname)
name.clear()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/470338.html