wxpython面板加载时会自动留下文字图片
Wxpython panel will automatically leave a text image when you load it
import wx
import sqlite3
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.mainPanel = wx.Panel(self)
self.data = ['111111','2222','333','4','555']
self.testcombo = wx.ComboBox(self.mainPanel,-1, choices=self.data, pos=(0,0))
# self.testcombo.Bind(wx.EVT_COMBOBOX, self.comboSave)
self.dataLabel = wx.StaticText(self.mainPanel,-1,("Nothing Saved Yet"),pos=(100,50))
self.dataLabel2 = wx.StaticText(self.mainPanel,-1, pos=(100,100))
self.testbut = wx.Button(self.mainPanel, wx.ID_SAVE, pos=(0,180))
self.testbut.Bind(wx.EVT_BUTTON, self.dataSave, id=wx.ID_SAVE )
self.conn = sqlite3.connect("test1.db")
self.cursor = self.conn.cursor()
self.cursor.execute('CREATE TABLE IF NOT EXISTS TEST(TEST CHAR(5))')
self.autorefresh()
# def comboSave(self, e):
# self.testcomboSave = self.testcombo.GetValue()
def dataSave(self, e):
self.testcomboSave = self.testcombo.GetValue()
self.cursor.execute('INSERT INTO TEST(TEST) VALUES(?)', (self.testcomboSave, ))
self.conn.commit()
self.dataLabel.SetLabel(self.testcomboSave)
def comboLoad(self):
self.cursor.execute('SELECT *FROM TEST')
for dbLoad in self.cursor.fetchall():pass
self.dbLoad = dbLoad[0]
self.dataLabel2.SetLabel(self.dbLoad)
def autorefresh(self):
self.comboLoad()
wx.CallLater(1000, self.autorefresh)
if __name__ == '__main__':
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
当我将组合框的内容加载到sqlite3中,然后自动将其加载到面板中时,文本中有一个残影。我不知道为什么。这是一个错误吗?
ex) 111111 -> 2222
这不是错误,您是将一段文字与另一段文字重叠。
为函数 comboLoad
中的每次迭代添加一个新的 wx.StaticText
并不是解决问题的方法。最初创建静态文本,然后随时更新它的值。
您还有冗余代码,您的 autorefresh
函数无缘无故每秒加载一次数据库内容,特别是因为数据库没有声明的键,这意味着您可以一遍又一遍地输入相同的值永远不会出错。
我已经把多余的代码注释掉了,这样你就可以看到,什么是必需的,什么是不需要的。
我相信这比我之前的 post.
更准确地满足您的需求
import wx
import sqlite3
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.mainPanel = wx.Panel(self)
self.data = ['111111','2222','333','4','555']
self.testcombo = wx.ComboBox(self.mainPanel,-1, choices=self.data, pos=(0,0))
# self.testcombo.Bind(wx.EVT_COMBOBOX, self.comboSave)
self.dataLabel = wx.StaticText(self.mainPanel,-1,("Nothing Saved Yet"),pos=(100,50))
self.testbut = wx.Button(self.mainPanel, wx.ID_SAVE, pos=(0,180))
self.testbut.Bind(wx.EVT_BUTTON, self.dataSave, id=wx.ID_SAVE )
self.conn = sqlite3.connect("test1.db")
self.cursor = self.conn.cursor()
self.cursor.execute('CREATE TABLE IF NOT EXISTS TEST(TEST CHAR(5))')
# self.autorefresh()
# def comboSave(self, e):
# self.testcomboSave = self.testcombo.GetValue()
def dataSave(self, e):
self.testcomboSave = self.testcombo.GetValue()
self.cursor.execute('INSERT INTO TEST(TEST) VALUES(?)', (self.testcomboSave, ))
self.conn.commit()
self.dataLabel.SetLabel("Item Saved to database: "+self.testcomboSave)
# def comboLoad(self):
# self.cursor.execute('SELECT *FROM TEST')
# for dbLoad in self.cursor.fetchall():pass
# wx.StaticText(self.mainPanel,label=dbLoad[0],pos=(100,100)
#
# def autorefresh(self):
# self.comboLoad()
# wx.CallLater(1000,self.autorefresh)
if __name__ == '__main__':
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
import wx
import sqlite3
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.mainPanel = wx.Panel(self)
self.data = ['111111','2222','333','4','555']
self.testcombo = wx.ComboBox(self.mainPanel,-1, choices=self.data, pos=(0,0))
# self.testcombo.Bind(wx.EVT_COMBOBOX, self.comboSave)
self.dataLabel = wx.StaticText(self.mainPanel,-1,("Nothing Saved Yet"),pos=(100,50))
self.dataLabel2 = wx.StaticText(self.mainPanel,-1, pos=(100,100))
self.testbut = wx.Button(self.mainPanel, wx.ID_SAVE, pos=(0,180))
self.testbut.Bind(wx.EVT_BUTTON, self.dataSave, id=wx.ID_SAVE )
self.conn = sqlite3.connect("test1.db")
self.cursor = self.conn.cursor()
self.cursor.execute('CREATE TABLE IF NOT EXISTS TEST(TEST CHAR(5))')
self.autorefresh()
# def comboSave(self, e):
# self.testcomboSave = self.testcombo.GetValue()
def dataSave(self, e):
self.testcomboSave = self.testcombo.GetValue()
self.cursor.execute('INSERT INTO TEST(TEST) VALUES(?)', (self.testcomboSave, ))
self.conn.commit()
self.dataLabel.SetLabel(self.testcomboSave)
def comboLoad(self):
self.cursor.execute('SELECT *FROM TEST')
for dbLoad in self.cursor.fetchall():pass
self.dbLoad = dbLoad[0]
self.dataLabel2.SetLabel(self.dbLoad)
def autorefresh(self):
self.comboLoad()
wx.CallLater(1000, self.autorefresh)
if __name__ == '__main__':
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
当我将组合框的内容加载到sqlite3中,然后自动将其加载到面板中时,文本中有一个残影。我不知道为什么。这是一个错误吗?
ex) 111111 -> 2222
这不是错误,您是将一段文字与另一段文字重叠。
为函数 comboLoad
中的每次迭代添加一个新的 wx.StaticText
并不是解决问题的方法。最初创建静态文本,然后随时更新它的值。
您还有冗余代码,您的 autorefresh
函数无缘无故每秒加载一次数据库内容,特别是因为数据库没有声明的键,这意味着您可以一遍又一遍地输入相同的值永远不会出错。
我已经把多余的代码注释掉了,这样你就可以看到,什么是必需的,什么是不需要的。
我相信这比我之前的 post.
import wx
import sqlite3
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.mainPanel = wx.Panel(self)
self.data = ['111111','2222','333','4','555']
self.testcombo = wx.ComboBox(self.mainPanel,-1, choices=self.data, pos=(0,0))
# self.testcombo.Bind(wx.EVT_COMBOBOX, self.comboSave)
self.dataLabel = wx.StaticText(self.mainPanel,-1,("Nothing Saved Yet"),pos=(100,50))
self.testbut = wx.Button(self.mainPanel, wx.ID_SAVE, pos=(0,180))
self.testbut.Bind(wx.EVT_BUTTON, self.dataSave, id=wx.ID_SAVE )
self.conn = sqlite3.connect("test1.db")
self.cursor = self.conn.cursor()
self.cursor.execute('CREATE TABLE IF NOT EXISTS TEST(TEST CHAR(5))')
# self.autorefresh()
# def comboSave(self, e):
# self.testcomboSave = self.testcombo.GetValue()
def dataSave(self, e):
self.testcomboSave = self.testcombo.GetValue()
self.cursor.execute('INSERT INTO TEST(TEST) VALUES(?)', (self.testcomboSave, ))
self.conn.commit()
self.dataLabel.SetLabel("Item Saved to database: "+self.testcomboSave)
# def comboLoad(self):
# self.cursor.execute('SELECT *FROM TEST')
# for dbLoad in self.cursor.fetchall():pass
# wx.StaticText(self.mainPanel,label=dbLoad[0],pos=(100,100)
#
# def autorefresh(self):
# self.comboLoad()
# wx.CallLater(1000,self.autorefresh)
if __name__ == '__main__':
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()