wx python 神秘地向 GUI 输出添加换行符
wx python mysteriously adding a newline character to GUI output
所以这里的目标是从用户那里获取输入并显示来自 excel sheet 的相关数据。
我们的问题是一个相当小但令人恼火的问题......
当输入未从数据库中获得匹配项时,我们将显示一条消息,指出输入的数字无效。我们使用辅助函数将输出格式化为 this
格式代码如下:
def makeRMAString(self, RMAnum, DMRnum, serial, status, cdate, udate):
rma_string = 'RMA#\t: {}\nDMR#\t: {} \nSerial#\t: {}\nStatus\t: {}\nCreated\t: {}\nUpdate\t: {}'
return rma_string.format(RMAnum, DMRnum, serial, status, cdate, udate)
屏幕截图显示,当输入与数据库中的内容匹配时,一切正常。如果没有,我们执行以下行:
self.rmaData.Label = self.makeRMAString('Invalid RMA number', '' , '', '', '', '')
这就是问题所在; 'Invalid RMA number' 没有得到格式良好的输出,而是被推入下一行,就像添加了下一行一样:
like this
我从命令提示符启动以查看输出,它看起来很好。 Here's proof
我们正在使用 wxpython 的 BoxSizer 来帮助:
self.rmaData = wx.StaticText(self.panel, wx.ID_ANY, self.makeRMAString('','','','','',''))
self.sizer_rmaHoriz = wx.BoxSizer(wx.HORIZONTAL)
self.sizer_rmaHoriz.Add(self.rmaData, 4, wx.ALIGN_LEFT|wx.CENTER, 15)
self.rmaData.SetFont(font)
我们正在使用 python 2.7.10
根据要求演示 Grid sizer。
我选了一个GridBagSizer
,好像最符合你的要求了。
import wx
data = [
["99550","539761","016833-0695","Shipped","03/27/20","Unknown"],
["99551","539761","xxxxxx-xxxx","Shipped","03/22/20","Unknown"],
["99554","539761","016833-0695","Warehouse 1","01/12/19","Unknown"],
['Invalid RMA number','','','','','',''],
["99555","539761","777777-0695","Warehouse 2","02/27/20","Unknown"],
["99556","539761","016833-XXXX","Shipped","03/27/20","03/24/20"],
['Invalid RMA number','','','','','','']
]
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title = title)
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.idx = 0
panel = wx.Panel(self)
sizer = wx.GridBagSizer(0,0)
self.rma = wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER)
sizer.Add(self.rma, pos = (0, 1), flag = wx.ALIGN_CENTRE_VERTICAL)
EnterB = wx.Button(panel, label = "Enter RMA")
sizer.Add(EnterB, pos = (0,2),flag = wx.ALL, border = 5)
text = wx.StaticText(panel, label = "RMA :")
sizer.Add(text, pos = (1, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc = wx.TextCtrl(panel, style=wx.TE_READONLY, size=(150,-1))
sizer.Add(self.tc, pos = (1, 1), flag = wx.EXPAND)
text1 = wx.StaticText(panel, label = "DMR :")
sizer.Add(text1, pos = (2, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc1 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc1, pos = (2,1), flag = wx.EXPAND)
text2 = wx.StaticText(panel,label = "Serial :")
sizer.Add(text2, pos = (3, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc2 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc2, pos = (3,1), flag = wx.EXPAND)
text3 = wx.StaticText(panel,label = "Status :")
sizer.Add(text3, pos = (4, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc3 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc3, pos = (4,1),flag = wx.EXPAND)
text4 = wx.StaticText(panel,label = "Created:")
sizer.Add(text4, pos = (5, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc4 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc4, pos = (5,1), flag = wx.EXPAND)
text5 = wx.StaticText(panel,label = "Update :")
sizer.Add(text5, pos = (6, 0), flag = wx.ALIGN_CENTRE_VERTICAL)
self.tc5 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc5, pos = (6,1), flag = wx.EXPAND)
Next = wx.Button(panel, label = "Demo")
sizer.Add(Next, pos = (7, 1))
panel.SetSizerAndFit(sizer)
EnterB.Bind(wx.EVT_BUTTON, self.OnRma)
self.rma.Bind(wx.EVT_TEXT_ENTER, self.OnRma)
Next.Bind(wx.EVT_BUTTON, self.OnNext)
def OnRma(self, event):
rma = self.rma.GetValue()
idx = [i for i, e in enumerate(data) if e[0] == rma]
if idx:
self.idx = idx[0]
self.OnNext(None)
else:
self.tc.SetValue('Invalid RMA number')
self.tc1.SetValue('')
self.tc2.SetValue('')
self.tc3.SetValue('')
self.tc4.SetValue('')
self.tc5.SetValue('')
def OnNext(self, event):
self.rma.SetValue('')
self.tc.SetValue(data[self.idx][0])
self.tc1.SetValue(data[self.idx][1])
self.tc2.SetValue(data[self.idx][2])
self.tc3.SetValue(data[self.idx][3])
self.tc4.SetValue(data[self.idx][4])
self.tc5.SetValue(data[self.idx][5])
self.idx += 1
if self.idx > len(data) - 1:
self.idx = 0
app = wx.App()
Example(None, title = 'GridBag Demo')
app.MainLoop()
用法:
- 输入 RMA 编号(99550、99551、99554、99555 或 99556)并按 Enter 或
Enter Rma
按钮
- 输入一些无效的 RMA 并按 Enter 或
Enter Rma
按钮
- 单击
Demo
按钮,它将滚动浏览数据
所以这里的目标是从用户那里获取输入并显示来自 excel sheet 的相关数据。 我们的问题是一个相当小但令人恼火的问题...... 当输入未从数据库中获得匹配项时,我们将显示一条消息,指出输入的数字无效。我们使用辅助函数将输出格式化为 this
格式代码如下:
def makeRMAString(self, RMAnum, DMRnum, serial, status, cdate, udate):
rma_string = 'RMA#\t: {}\nDMR#\t: {} \nSerial#\t: {}\nStatus\t: {}\nCreated\t: {}\nUpdate\t: {}'
return rma_string.format(RMAnum, DMRnum, serial, status, cdate, udate)
屏幕截图显示,当输入与数据库中的内容匹配时,一切正常。如果没有,我们执行以下行:
self.rmaData.Label = self.makeRMAString('Invalid RMA number', '' , '', '', '', '')
这就是问题所在; 'Invalid RMA number' 没有得到格式良好的输出,而是被推入下一行,就像添加了下一行一样: like this
我从命令提示符启动以查看输出,它看起来很好。 Here's proof
我们正在使用 wxpython 的 BoxSizer 来帮助:
self.rmaData = wx.StaticText(self.panel, wx.ID_ANY, self.makeRMAString('','','','','',''))
self.sizer_rmaHoriz = wx.BoxSizer(wx.HORIZONTAL)
self.sizer_rmaHoriz.Add(self.rmaData, 4, wx.ALIGN_LEFT|wx.CENTER, 15)
self.rmaData.SetFont(font)
我们正在使用 python 2.7.10
根据要求演示 Grid sizer。
我选了一个GridBagSizer
,好像最符合你的要求了。
import wx
data = [
["99550","539761","016833-0695","Shipped","03/27/20","Unknown"],
["99551","539761","xxxxxx-xxxx","Shipped","03/22/20","Unknown"],
["99554","539761","016833-0695","Warehouse 1","01/12/19","Unknown"],
['Invalid RMA number','','','','','',''],
["99555","539761","777777-0695","Warehouse 2","02/27/20","Unknown"],
["99556","539761","016833-XXXX","Shipped","03/27/20","03/24/20"],
['Invalid RMA number','','','','','','']
]
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title = title)
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.idx = 0
panel = wx.Panel(self)
sizer = wx.GridBagSizer(0,0)
self.rma = wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER)
sizer.Add(self.rma, pos = (0, 1), flag = wx.ALIGN_CENTRE_VERTICAL)
EnterB = wx.Button(panel, label = "Enter RMA")
sizer.Add(EnterB, pos = (0,2),flag = wx.ALL, border = 5)
text = wx.StaticText(panel, label = "RMA :")
sizer.Add(text, pos = (1, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc = wx.TextCtrl(panel, style=wx.TE_READONLY, size=(150,-1))
sizer.Add(self.tc, pos = (1, 1), flag = wx.EXPAND)
text1 = wx.StaticText(panel, label = "DMR :")
sizer.Add(text1, pos = (2, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc1 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc1, pos = (2,1), flag = wx.EXPAND)
text2 = wx.StaticText(panel,label = "Serial :")
sizer.Add(text2, pos = (3, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc2 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc2, pos = (3,1), flag = wx.EXPAND)
text3 = wx.StaticText(panel,label = "Status :")
sizer.Add(text3, pos = (4, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc3 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc3, pos = (4,1),flag = wx.EXPAND)
text4 = wx.StaticText(panel,label = "Created:")
sizer.Add(text4, pos = (5, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc4 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc4, pos = (5,1), flag = wx.EXPAND)
text5 = wx.StaticText(panel,label = "Update :")
sizer.Add(text5, pos = (6, 0), flag = wx.ALIGN_CENTRE_VERTICAL)
self.tc5 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc5, pos = (6,1), flag = wx.EXPAND)
Next = wx.Button(panel, label = "Demo")
sizer.Add(Next, pos = (7, 1))
panel.SetSizerAndFit(sizer)
EnterB.Bind(wx.EVT_BUTTON, self.OnRma)
self.rma.Bind(wx.EVT_TEXT_ENTER, self.OnRma)
Next.Bind(wx.EVT_BUTTON, self.OnNext)
def OnRma(self, event):
rma = self.rma.GetValue()
idx = [i for i, e in enumerate(data) if e[0] == rma]
if idx:
self.idx = idx[0]
self.OnNext(None)
else:
self.tc.SetValue('Invalid RMA number')
self.tc1.SetValue('')
self.tc2.SetValue('')
self.tc3.SetValue('')
self.tc4.SetValue('')
self.tc5.SetValue('')
def OnNext(self, event):
self.rma.SetValue('')
self.tc.SetValue(data[self.idx][0])
self.tc1.SetValue(data[self.idx][1])
self.tc2.SetValue(data[self.idx][2])
self.tc3.SetValue(data[self.idx][3])
self.tc4.SetValue(data[self.idx][4])
self.tc5.SetValue(data[self.idx][5])
self.idx += 1
if self.idx > len(data) - 1:
self.idx = 0
app = wx.App()
Example(None, title = 'GridBag Demo')
app.MainLoop()
用法:
- 输入 RMA 编号(99550、99551、99554、99555 或 99556)并按 Enter 或
Enter Rma
按钮 - 输入一些无效的 RMA 并按 Enter 或
Enter Rma
按钮 - 单击
Demo
按钮,它将滚动浏览数据