如何从 textctrl 获取输入的文本以在 wxpython 中输出答案

发布于 2024-12-24 22:07:16 字数 5199 浏览 0 评论 0原文

我真正需要帮助的是第 112 行的函数。我正在尝试创建一个 GUI 计算器,唯一遇到问题的部分是当人输入像 5+5 这样的问题时,它无法将其存储在变量中。那么我该如何解决这个问题,然后将其输出到文本框。 谢谢

import math
import wx

class justin(wx.Frame):

    loop=False   

    def __init__(self,parent,id):

        wx.Frame.__init__(self,parent,id,'MAP2', size=(250, 300))
        panel=wx.Panel(self)

        status=self.CreateStatusBar()
        file_menu = wx.MenuBar()
        menu1=wx.Menu()
        menu2=wx.Menu()
        exit1=menu1.Append(wx.NewId(),"exit","exit")
        info1=menu1.Append(wx.NewId(),"info","info")
        instr=menu2.Append(wx.NewId(),"instructions","instructions")
        file_menu.Append(menu1,"file")
        file_menu.Append(menu2,"instructions")
        self.SetMenuBar(file_menu)

        self.panel = panel

        self.Bind(wx.EVT_MENU, self.onexit, exit1)
        self.Bind(wx.EVT_MENU, self.oninfo, info1)
        self.Bind(wx.EVT_MENU, self.oninstr, instr)

        self.box=wx.TextCtrl(panel,-1,"",(0,0),(180,45))        

        self.button1=wx.Button(panel,label='1', size=(45,45))
        self.button2=wx.Button(panel,label='2', size=(45,45))
        self.button3=wx.Button(panel,label='3', size=(45,45))
        self.button4=wx.Button(panel,label='4', size=(45,45))
        self.button5=wx.Button(panel,label='5', size=(45,45))
        self.button6=wx.Button(panel,label='6', size=(45,45))
        self.button7=wx.Button(panel,label='7', size=(45,45))
        self.button8=wx.Button(panel,label='8', size=(45,45))
        self.button9=wx.Button(panel,label='9', size=(45,45))
        self.buttonplus=wx.Button(panel,label='+', size=(45,45))
        self.buttonsub=wx.Button(panel,label='-', size=(45,45))
        self.buttonmul=wx.Button(panel,label='*', size=(45,45))
        self.buttondiv=wx.Button(panel,label='/', size=(45,45))
        self.button0=wx.Button(panel,label='0', size=(45,45))
        self.enter=wx.Button(panel,label='enter',size=(45,45))

        siz=wx.GridBagSizer(5,5)
        siz.Add(self.button1,pos=(2,0))
        siz.Add(self.button2,pos=(2,1))
        siz.Add(self.button3,pos=(2,2))
        siz.Add(self.button4,pos=(3,0))
        siz.Add(self.button5,pos=(3,1))
        siz.Add(self.button6,pos=(3,2))
        siz.Add(self.button7,pos=(4,0))
        siz.Add(self.button8,pos=(4,1))
        siz.Add(self.button9,pos=(4,2))
        siz.Add(self.buttonplus,pos=(2,3))
        siz.Add(self.buttonsub,pos=(3,3))
        siz.Add(self.buttonmul,pos=(4,3))
        siz.Add(self.buttondiv,pos=(5,3))
        siz.Add(self.button0,pos=(5,2))
        siz.Add(self.enter,pos=(5,0))

        panel.SetSizer(siz)

        self.Bind(wx.EVT_BUTTON,self.ebutton1,self.button1)
        self.Bind(wx.EVT_BUTTON,self.ebutton2,self.button2)
        self.Bind(wx.EVT_BUTTON,self.ebutton3,self.button3)
        self.Bind(wx.EVT_BUTTON,self.ebutton4,self.button4)
        self.Bind(wx.EVT_BUTTON,self.ebutton5,self.button5)
        self.Bind(wx.EVT_BUTTON,self.ebutton6,self.button6)
        self.Bind(wx.EVT_BUTTON,self.ebutton7,self.button7)
        self.Bind(wx.EVT_BUTTON,self.ebutton8,self.button8)
        self.Bind(wx.EVT_BUTTON,self.ebutton9,self.button9)
        self.Bind(wx.EVT_BUTTON,self.ebutton0,self.button0)
        self.Bind(wx.EVT_BUTTON,self.ebuttonplus,self.buttonplus)
        self.Bind(wx.EVT_BUTTON,self.ebuttonsub,self.buttonsub)
        self.Bind(wx.EVT_BUTTON,self.ebuttonmul,self.buttonmul)
        self.Bind(wx.EVT_BUTTON,self.ebuttondiv,self.buttondiv)
        self.Bind(wx.EVT_BUTTON,self.eenter,self.enter)


    def ebutton1(self,e):
        self.box.AppendText('1')
    def ebutton2(self,e):
        self.box.AppendText('2')
    def ebutton3(self,e):
        self.box.AppendText('3')
    def ebutton4(self,e):
        self.box.AppendText('4')
    def ebutton5(self,e):
        self.box.AppendText('5')
    def ebutton6(self,e):
        self.box.AppendText('6')
    def ebutton7(self,e):
        self.box.AppendText('7')
    def ebutton8(self,e):
        self.box.AppendText('8')
    def ebutton9(self,e):
        self.box.AppendText('9')
    def ebutton0(self,e):
        self.box.AppendText('0')
    def ebuttonplus(self,e):
        self.box.AppendText('+')
    def ebuttonsub(self,e):
        self.box.AppendText('-')
    def ebuttonmul(self,e):
        self.box.AppendText('*')
    def ebuttondiv(self,e):
        self.box.AppendText('/')
    def eenter(self,e):
        a=self.box.GetValue()
        answer=int(a)
        ans=str(answer)
        self.box.SetValue(ans)


    def onexit(self,e):
        self.Close()

    def oninfo(self,e):
        dlg = wx.MessageDialog(None,"This is a program that computes percent error value. MAP2 is created by Justin Rolf","info",wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def oninstr(self,e):
        dlg1 = wx.MessageDialog(None,"To use this program enter your first number in the top gray box (make sure to have all text deleted before you type), then enter your second number in the bottom box, and hit the enter button","instructions",wx.OK)
        dlg1.ShowModal()
        dlg1.Destroy()


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

what i really need help with is the function on line 112. I am trying to create a GUI calculator the only part i am having trouble with is when the person enters a problem like 5+5 it isnt able to store that in a varible. So how would i solve the problem and then output it to the text box.
thank you

import math
import wx

class justin(wx.Frame):

    loop=False   

    def __init__(self,parent,id):

        wx.Frame.__init__(self,parent,id,'MAP2', size=(250, 300))
        panel=wx.Panel(self)

        status=self.CreateStatusBar()
        file_menu = wx.MenuBar()
        menu1=wx.Menu()
        menu2=wx.Menu()
        exit1=menu1.Append(wx.NewId(),"exit","exit")
        info1=menu1.Append(wx.NewId(),"info","info")
        instr=menu2.Append(wx.NewId(),"instructions","instructions")
        file_menu.Append(menu1,"file")
        file_menu.Append(menu2,"instructions")
        self.SetMenuBar(file_menu)

        self.panel = panel

        self.Bind(wx.EVT_MENU, self.onexit, exit1)
        self.Bind(wx.EVT_MENU, self.oninfo, info1)
        self.Bind(wx.EVT_MENU, self.oninstr, instr)

        self.box=wx.TextCtrl(panel,-1,"",(0,0),(180,45))        

        self.button1=wx.Button(panel,label='1', size=(45,45))
        self.button2=wx.Button(panel,label='2', size=(45,45))
        self.button3=wx.Button(panel,label='3', size=(45,45))
        self.button4=wx.Button(panel,label='4', size=(45,45))
        self.button5=wx.Button(panel,label='5', size=(45,45))
        self.button6=wx.Button(panel,label='6', size=(45,45))
        self.button7=wx.Button(panel,label='7', size=(45,45))
        self.button8=wx.Button(panel,label='8', size=(45,45))
        self.button9=wx.Button(panel,label='9', size=(45,45))
        self.buttonplus=wx.Button(panel,label='+', size=(45,45))
        self.buttonsub=wx.Button(panel,label='-', size=(45,45))
        self.buttonmul=wx.Button(panel,label='*', size=(45,45))
        self.buttondiv=wx.Button(panel,label='/', size=(45,45))
        self.button0=wx.Button(panel,label='0', size=(45,45))
        self.enter=wx.Button(panel,label='enter',size=(45,45))

        siz=wx.GridBagSizer(5,5)
        siz.Add(self.button1,pos=(2,0))
        siz.Add(self.button2,pos=(2,1))
        siz.Add(self.button3,pos=(2,2))
        siz.Add(self.button4,pos=(3,0))
        siz.Add(self.button5,pos=(3,1))
        siz.Add(self.button6,pos=(3,2))
        siz.Add(self.button7,pos=(4,0))
        siz.Add(self.button8,pos=(4,1))
        siz.Add(self.button9,pos=(4,2))
        siz.Add(self.buttonplus,pos=(2,3))
        siz.Add(self.buttonsub,pos=(3,3))
        siz.Add(self.buttonmul,pos=(4,3))
        siz.Add(self.buttondiv,pos=(5,3))
        siz.Add(self.button0,pos=(5,2))
        siz.Add(self.enter,pos=(5,0))

        panel.SetSizer(siz)

        self.Bind(wx.EVT_BUTTON,self.ebutton1,self.button1)
        self.Bind(wx.EVT_BUTTON,self.ebutton2,self.button2)
        self.Bind(wx.EVT_BUTTON,self.ebutton3,self.button3)
        self.Bind(wx.EVT_BUTTON,self.ebutton4,self.button4)
        self.Bind(wx.EVT_BUTTON,self.ebutton5,self.button5)
        self.Bind(wx.EVT_BUTTON,self.ebutton6,self.button6)
        self.Bind(wx.EVT_BUTTON,self.ebutton7,self.button7)
        self.Bind(wx.EVT_BUTTON,self.ebutton8,self.button8)
        self.Bind(wx.EVT_BUTTON,self.ebutton9,self.button9)
        self.Bind(wx.EVT_BUTTON,self.ebutton0,self.button0)
        self.Bind(wx.EVT_BUTTON,self.ebuttonplus,self.buttonplus)
        self.Bind(wx.EVT_BUTTON,self.ebuttonsub,self.buttonsub)
        self.Bind(wx.EVT_BUTTON,self.ebuttonmul,self.buttonmul)
        self.Bind(wx.EVT_BUTTON,self.ebuttondiv,self.buttondiv)
        self.Bind(wx.EVT_BUTTON,self.eenter,self.enter)


    def ebutton1(self,e):
        self.box.AppendText('1')
    def ebutton2(self,e):
        self.box.AppendText('2')
    def ebutton3(self,e):
        self.box.AppendText('3')
    def ebutton4(self,e):
        self.box.AppendText('4')
    def ebutton5(self,e):
        self.box.AppendText('5')
    def ebutton6(self,e):
        self.box.AppendText('6')
    def ebutton7(self,e):
        self.box.AppendText('7')
    def ebutton8(self,e):
        self.box.AppendText('8')
    def ebutton9(self,e):
        self.box.AppendText('9')
    def ebutton0(self,e):
        self.box.AppendText('0')
    def ebuttonplus(self,e):
        self.box.AppendText('+')
    def ebuttonsub(self,e):
        self.box.AppendText('-')
    def ebuttonmul(self,e):
        self.box.AppendText('*')
    def ebuttondiv(self,e):
        self.box.AppendText('/')
    def eenter(self,e):
        a=self.box.GetValue()
        answer=int(a)
        ans=str(answer)
        self.box.SetValue(ans)


    def onexit(self,e):
        self.Close()

    def oninfo(self,e):
        dlg = wx.MessageDialog(None,"This is a program that computes percent error value. MAP2 is created by Justin Rolf","info",wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def oninstr(self,e):
        dlg1 = wx.MessageDialog(None,"To use this program enter your first number in the top gray box (make sure to have all text deleted before you type), then enter your second number in the bottom box, and hit the enter button","instructions",wx.OK)
        dlg1.ShowModal()
        dlg1.Destroy()


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[浮城] 2024-12-31 22:07:16

我认为你的问题在这里:

def eenter(self,e):
    a=self.box.GetValue()
    answer=int(a)
    ans=str(answer)
    self.box.SetValue(ans)

它不起作用,因为 a 是一个不能直接转换为整数的字符串。
你可以看到这里是一个wxPython计算器的例子< /a>.它只需要不到 50 行代码,因此您可以从这段代码中学到很多东西。另一个例子是这里
你会发现他们使用 python 内置函数 eval 来处理输入的字符串。对于您的情况,这转化为:

answer = eval(a)

通常不建议将 eval 用于一般代码。当它执行它获取的任何字符串时,它被认为是危险的,但是,对于您的情况,它是一个有效的替代方案(另一种方法是解析输入字符串并使用 python 运算符 方法或自定义函数)。

I think your problem is here:

def eenter(self,e):
    a=self.box.GetValue()
    answer=int(a)
    ans=str(answer)
    self.box.SetValue(ans)

It doesn't work because a is a string that can not be converted to an integer directly.
You can see here an example of a wxPython calculator. It takes less than 50 lines of code so you can learn a lot from this code. Another example is here
You will discover they use python built-in function eval to process the entered string. For your case this translates into:

answer = eval(a)

eval is often not recommended for general code. As it executes any string it gets, it is considered dangerous, However, for your case, it is a valid alternative (another would be to parse the input string and bind operator chars with python operator methods or custom functions).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文