问题
I am able to update the tKinter entry widgets boxes using textvariables... the issue is that it add brackets '{}' in my desired data...
def showRecord(self):
connection = sqlite3.connect("../employee.db")
connection.text_factory = sqlite3.OptimizedUnicode
cursor = connection.cursor ()
cursor.execute ( '''SELECT "Scheduled Shift" FROM employee_details WHERE Ecode = "5568328"''' )
items = cursor.fetchall ()
self.Employee1_FirstDay_ActualShift.set(items[0])
self.Employee1_SecondDay_ActualShift.set(items[1])
self.Employee1_ThirdDay_ActualShift.set(items[2])
self.Employee1_FourthDay_ActualShift.set(items[3])
self.Employee1_FifthDay_ActualShift.set(items[4])
self.Employee1_SixthDay_ActualShift.set(items[5])
self.Employee1_SeventhDay_ActualShift.set(items[6])
connection.commit ()
connection.close ()
Seeking help pls... Need to remove those brackets as shown in fig.
回答1:
The reason it is doing that is because you are setting the value of a string variable to a list. Tkinter is a thin wrapper around a tcl/tk interpreter, and tcl uses curly braces to preserve the list structure when converting the list to a string when a list element has spaces or other special characters.
The solution is to make sure you pass a string to the set
method. Otherewise the list will be passed to tcl/tk and it will use it's own list-to-string conversion.
In your case, since items
is a list (rows) of lists (columns) and each row has a single column, you would do something like this to insert column zero of row zero into self.Employee1_FirstDay_ActualShift
:
row_0 = items[0]
col_0 = row_0[0]
self.Employee1_FirstDay_ActualShift.set(col_0)
To condense that to one line, combined with all of the other rows it would look something like the following. I've added some extra whitespace to make it easier to compare each line. Also, this assumes that items
has seven rows, and each row has at least one column.
self.Employee1_FirstDay_ActualShift.set( items[0][0])
self.Employee1_SecondDay_ActualShift.set( items[1][0])
self.Employee1_ThirdDay_ActualShift.set( items[2][0])
self.Employee1_FourthDay_ActualShift.set( items[3][0])
self.Employee1_FifthDay_ActualShift.set( items[4][0])
self.Employee1_SixthDay_ActualShift.set( items[5][0])
self.Employee1_SeventhDay_ActualShift.set(items[6][0])
来源:https://stackoverflow.com/questions/62416358/tkinter-entry-widget-updates-values-in-curli-brackets