给生肖计算器加上了图形用户界面,可是盒子布局管理器不知道为什么有问题
# coding=utf-8
import wx
class ADCal(wx.Frame):
def cal(self, event):
year = text_box.GetValue()
if year.isdigit() == True:
int(year)
else:
self.result = wx.SetLabelText('请输入整数')
# 计算公元年份和公元前年份生肖的表达式与所有生肖
AA = (year - 1) % 12
AB = (year - 1) % 10
AD = ["鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴"]
# 解决year为0导致的异常
try:
# 判断年份为公元后还是公元前并输出结果
if year >= 1:
wx.SetLabelText('生肖:{}'.format(AD[AA]))
else:
wx.SetLabelText('生肖:{}'.format(AD[AB + 1]))
except IndexError as e:
self.result = wx.SetLabelText('没有公元0年')
finally:
finally = ''
def __init__(self):
super().__init__(None, title='生肖计算器', size=(280, 110), pos=(0, 0))
panel = wx.Panel(parent=self)
self.caption = wx.StaticText(parent=panel, label='输入公元前年份需加上负号,年份不能为0,输入q退出。')
self.text_type = wx.StaticText(parent=panel, label='年份:')
text_box = wx.TextCtrl(panel)
button = wx.Button(parent=panel, label='计算')
self.Bind(wx.EVT_BUTTON, self.cal, button)
self.result_type = wx.StaticText(parent=panel, label='生肖:')
self.result = wx.StaticText(parent=panel, label='')
# 布局管理
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(text_box, proportion=2, flag=wx.EXPAND | wx.ALL, border=10)
hbox.Add(button, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.caption, proportion=1, flag=wx.ALIGN_LEFT | wx.FIXED_MINSIZE | wx.ALL, border=10)
vbox.Add(self.text_type, proportion=1, flag=wx.ALIGN_LEFT | wx.FIXED_MINSIZE | wx.LEFT, border=10)
vbox.Add(hbox, proportion=1, flag=wx.ALIGN_LEFT)
vbox.Add(self.result_type, proportion=1, flag=wx.ALIGN_LEFT | wx.FIXED_MINSIZE | wx.ALL, border=10)
vbox.Add(self.result, proportion=1, flag=wx.ALIGN_LEFT | wx.FIXED_MINSIZE | wx.LEFT, border=10)
app = wx.App()
frm = ADCal()
frm.Show()
app.MainLoop()
控件位置不对