标题:求助:在wxpython中使用if 条件,运行结果不正确。求高手帮我,谢谢。
只看楼主
wangjx236007
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-2
结帖率:40%
已结贴  问题点数:20 回复次数:2 
求助:在wxpython中使用if 条件,运行结果不正确。求高手帮我,谢谢。
程序代码:
import wx
import datetime

num=19.2
class MyFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'地铁充值',size=(280,300))
        panel=wx.Panel(self)
        self.zhan=wx.StaticText(panel,label='请输站名')
        self.zhan1=wx.TextCtrl(panel,style=wx.TE_LEFT)
        hsize=wx.BoxSizer(wx.HORIZONTAL)
        hsize.Add(self.zhan,proportion=0,flag=wx.ALL,border=10)
        hsize.Add(self.zhan1,proportion=0,flag=wx.ALL,border=10)
        self.mony=wx.StaticText(panel,label='充值金额')
        self.mony1=wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT_ENTER, self.text_enter,self.mony1)
        hsize1=wx.BoxSizer(wx.HORIZONTAL)
        hsize1.Add(self.mony,proportion=0,flag=wx.ALL,border=10)
        hsize1.Add(self.mony1, proportion=0, flag=wx.ALL, border=10)
        self.jg=wx.StaticText(panel,label='',size=(150,250))

        #hsize2=wx.BoxSizer(wx.HORIZONTAL)
        #hsize2.Add(self.jg,proportion=0,flag=wx.ALIGN_CENTER,border=5)
        vsize=wx.BoxSizer(wx.VERTICAL)
        vsize.Add(hsize,proportion=0,flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=5)
        vsize.Add(hsize1, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5)
        vsize.Add(self.jg, proportion=0, flag=wx.ALIGN_CENTER, border=5)
        panel.SetSizer(vsize)
    def text_enter(self,event):
        zha=self.zhan1.GetValue()
        mooy=self.mony1.GetValue()
        with open('aaa.txt','r',encoding='utf-8') as file:
            while True:
                lis=file.readline()
                if not lis:
                    break
                else:
                    qq=lis.split('|')
                    bb=qq[0]
                    cc=qq[1]
                    dd=qq[2]
                    if bb==zha:
                        wjx=f'车站名称:{bb}\n设备编号:{cc}\n票卡编号:{dd}\n'

                            #''.format(dd)
                            #'充值时间:', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                            #'充前余额:{}'.format(num)
                            #'充值额是:{}'.format(mooy)
                            #'当前余额:{}'.format(num + mooy))


                        self.jg.SetLabel(wjx)
                    else:
                        py = "站名输入错误,请重新输入"
                        self.jg.SetLabel(py)
                        self.zhan1.SetValue("")
                        self.mony1.SetValue("")






if __name__ == '__main__':
    app=wx.App()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
搜索更多相关主题的帖子: flag panel if border Add 
2021-03-13 11:25
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:20 
回复 楼主 wangjx236007
命中后需要跳出循环,否则会一直匹配。直到最后一行以下面的else的结果返回

程序代码:
import wx
import datetime

num=19.2
class MyFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'地铁充值',size=(280,300))
        panel=wx.Panel(self)
        self.zhan=wx.StaticText(panel,label='请输站名')
        self.zhan1=wx.TextCtrl(panel,style=wx.TE_LEFT)
        hsize=wx.BoxSizer(wx.HORIZONTAL)
        hsize.Add(self.zhan,proportion=0,flag=wx.ALL,border=10)
        hsize.Add(self.zhan1,proportion=0,flag=wx.ALL,border=10)
        self.mony=wx.StaticText(panel,label='充值金额')
        self.mony1=wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT_ENTER, self.text_enter,self.mony1)
        hsize1=wx.BoxSizer(wx.HORIZONTAL)
        hsize1.Add(self.mony,proportion=0,flag=wx.ALL,border=10)
        hsize1.Add(self.mony1, proportion=0, flag=wx.ALL, border=10)
        self.jg=wx.StaticText(panel,label='',size=(150,250))

        #hsize2=wx.BoxSizer(wx.HORIZONTAL)
        #hsize2.Add(self.jg,proportion=0,flag=wx.ALIGN_CENTER,border=5)
        vsize=wx.BoxSizer(wx.VERTICAL)
        vsize.Add(hsize,proportion=0,flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=5)
        vsize.Add(hsize1, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5)
        vsize.Add(self.jg, proportion=0, flag=wx.ALIGN_CENTER, border=5)
        panel.SetSizer(vsize)
    def text_enter(self,event):
        zha=self.zhan1.GetValue()
        mooy=self.mony1.GetValue()
        with open('aaa.txt','r',encoding='utf-8') as file:
            while True:
                lis=file.readline()
                if not lis:
                    break
                else:
                    qq=lis.split('|')
                    bb=qq[0]
                    cc=qq[1]
                    dd=qq[2]
                    if bb==zha:
                        wjx=f'车站名称:{bb}\n设备编号:{cc}\n票卡编号:{dd}\n'

                            #''.format(dd)
                            #'充值时间:', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                            #'充前余额:{}'.format(num)
                            #'充值额是:{}'.format(mooy)
                            #'当前余额:{}'.format(num + mooy))


                        self.jg.SetLabel(wjx)
                        break   ##这里如果不跳出循环会一直匹配。直到最后一行以下面的else的结果返回
                    else:
                        py = "站名输入错误,请重新输入"
                        self.jg.SetLabel(py)
                        self.zhan1.SetValue("")
                        self.mony1.SetValue("")






if __name__ == '__main__':
    app=wx.App()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()


[此贴子已经被作者于2021-3-15 11:08编辑过]

2021-03-15 11:07
sssooosss
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:3
帖 子:664
专家分:1115
注 册:2019-8-27
得分:0 
共同学习
2021-03-17 10:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505110-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.254014 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved