import wx
# noinspection PyUnresolvedReferences,PyTypeChecker
class NumberFrame(wx.Frame):
reading_list = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
level_list = ['', '万', '亿', '万亿', '兆', '万兆', '亿兆', '万亿兆', '京']
unit_list = ['', '十', '百', '千']
dict_list = [1 * 10 ** i for i in range(33)]
def __init__(self, panel, id):
wx.Frame.__init__(self, panel, id, '数字大小写转换', size=(400, 500))
# 创建面板
panel = wx.Panel(self)
self.bt_confirm = wx.Button(panel, label='确定')
self.bt_confirm.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
# self.bt_redo = wx.Button(panel, label='重新输入')
# self.bt_redo.Bind(wx.EVT_BUTTON, self.OnclickCancel)
# self.bt_end = wx.Button(panel, label='结束')
# self.bt_end.Bind(wx.EVT_BUTTON, self.OnclickCancel)
self.title = wx.StaticText(panel, label='数字转大写界面')
self.label_user = wx.StaticText(panel, label='请输入数字')
self.text_user = wx.TextCtrl(panel, style=wx.TE_LEFT|wx.TE_PROCESS_ENTER) #输入数字文本框
self.text_user.Bind(wx.EVT_TEXT_ENTER,self.OnclickSubmit)
self.output_title = wx.StaticText(panel, label='转换结果:')
self.output_text = wx.TextCtrl(panel, style=wx.TE_READONLY | wx.RIGHT|wx.TE_NOHIDESEL) #转换结果文本框
# 添加容器
hsizer_user = wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user, proportion=0, flag=wx.ALL, border=5)
hsizer_user.Add(self.text_user, proportion=1, flag=wx.ALL, border=5)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm, proportion=0, flag=wx.ALIGN_CENTER, border=5)
# hsizer_button.Add(self.bt_redo, proportion=0, flag=wx.ALIGN_CENTER, border=5)
# hsizer_button.Add(self.bt_end, proportion=0, flag=wx.ALIGN_CENTER, border=5)
vsizer_all = wx.BoxSizer(wx.VERTICAL) # 添加夙竖向控件到容器
vsizer_all.Add(self.title, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER, border=15)
vsizer_all.Add(hsizer_user, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER, border=15)
vsizer_all.Add(hsizer_button, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER, border=15)
vsizer_all.Add(self.output_title, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER, border=15)
vsizer_all.Add(self.output_text, proportion=0, flag=wx.EXPAND, border=15)
panel.SetSizer(vsizer_all)
def get_bignum(text_user): # list
self = text_user[::-1] # 1234--4321
length = len(self) # 4
len_list = length # 4
list1 = []
number_int = int(self)
for i in range(length):
len_list += -1
m = number_int // NumberFrame.dict_list[len_list]
number_int = number_int % NumberFrame.dict_list[len_list]
list1.append(NumberFrame.reading_list[m])
return list1
def verse_number(self):
count = 0
group1_number = []
group2_number = []
for i in self:
count += 1
group1_number.append(i)
if count % 4 == 0:
group2_number.append(group1_number)
group1_number = []
if group1_number not in group2_number and len(group1_number) != 0:
group2_number.append(group1_number)
return group2_number
def checkzero(self):
number = -1
for checklist in self:
count = checklist.count('零')
number += 1
if len(self) <= 1:
if count == 1:
if len(checklist) == 1:
if checklist[0] == '零':
checklist[0] = '零'
self[number] = checklist
else:
if checklist[0] == '零':
checklist[0] = ''
self[number] = checklist
if count == 2:
if checklist[0] == '零':
checklist[0] = ''
self[number] = checklist
elif checklist[0:2] == ['零', '零']: # 12345600
checklist[0:2] = ['', '']
self[number] = checklist
elif checklist[1:3] == ['零', '零']: # 12345006
checklist[1:3] = ['', '零']
self[number] = checklist
elif checklist[2:4] == ['零', '零']: # 12340056
checklist[2:4] = ['', '零']
self[number] = checklist
else: # level2
if count == 1:
if checklist[0] == '零':
checklist[0] = ''
if count == 2:
if checklist[0] == '零':
checklist[0] = ''
if checklist[0:2] == ['', '零']: # 12345600
checklist[0:2] = ['', '']
self[number] = checklist
elif checklist[0:2] == ['零', '零']: # 12345600
checklist[0:2] = ['', '']
self[number] = checklist
elif checklist[1:3] == ['零', '零']: # 12345006
checklist[1:3] = ['', '零']
self[number] = checklist
elif checklist[2:4] == ['零', '零']: # 12340056
checklist[2:4] = ['', '零']
self[number] = checklist
if count == 3:
if checklist[0] != '零':
checklist[1:3] = ['', '', '']
self[number] = checklist
elif checklist[1] != '零':
checklist[0] = ''
checklist[2] = ''
self[number] = checklist
elif checklist[2] != '零':
checklist[0:2] = ['', '零']
self[number] = checklist
elif checklist[3] != ['零']:
checklist[0:3] = ['', '', '']
self[number] = checklist
if count == 4:
checklist[0:4] = ['', '', '', '']
self[number] = checklist
else:
return self
def unitnumber(self):
company = ''
template = []
list_reverse = self[::-1]
level_number = len(list_reverse)
template_number = level_number
template_zero = -1
for i in list_reverse:
jishu_number = len(i)
template_number += -1
template.append(NumberFrame.level_list[template_number])
level_number += -1
template_zero += 1
for word in reversed(i): # [一二三四]
jishu_number += -1
if jishu_number == 0: # 掌管第一位数字以及level
if i == ['', '', '', '']: # 去掉level级别,一亿则去除万和整
company = company + word + NumberFrame.unit_list[jishu_number]
company = company + NumberFrame.level_list[level_number]
company_new = company.replace(template[template_zero], '')
company = company_new
else:
company = company + word + NumberFrame.unit_list[jishu_number]
company = company + NumberFrame.level_list[level_number]
else:
if word == '零' or word == '':
company = company + word
elif len(i) == 2 and i[1] == '一': # 10 100000
company = company + NumberFrame.unit_list[jishu_number]
else:
company = company + word + NumberFrame.unit_list[jishu_number]
return company
def OnclickSubmit(self, event):
"""单击 “确定” 按钮,执行方法
"""
checking_blank = self.text_user.GetValue().startswith(' ')
if checking_blank is False:
if str(self.text_user.GetValue()).startswith('0') is True:
if len(str(self.text_user.GetValue())) == 1:
user_number = NumberFrame.unitnumber(NumberFrame.checkzero(
NumberFrame.verse_number(NumberFrame.get_bignum(self.text_user.GetValue()))))
self.output_text.SetValue(user_number)
else:
message = '输入有误!'
wx.MessageBox(message)
else:
checking = str(self.text_user.GetValue()).isdecimal()
if checking is True:
user_number = NumberFrame.unitnumber(
NumberFrame.checkzero(NumberFrame.verse_number(NumberFrame.get_bignum(self.text_user.GetValue()))))
self.output_text.SetValue(user_number)
else:
message = '输入有误!'
wx.MessageBox(message)
else:
message = '输入有误'
wx.MessageBox(message)
def OnEnter(self):
print(1)
if __name__ == '__main__':
app = wx.App()
frame = NumberFrame(panel=None, id=-1)
frame.Show()
app.MainLoop()
?
|