【问题标题】:wxPython, error when trying to get value from textctrlwxPython,尝试从 textctrl 获取值时出错
【发布时间】:2015-04-09 09:36:14
【问题描述】:

我正在努力学习 Python 和 wxPython;我正在制作一个简单的窗口,应该在控制台上打印 textctrl 元素的内容。但是,我收到此错误:

AttributeError: 'Frame' object has no attribute 't_username'

这是代码中感兴趣的部分:

import sys, wx
app = wx.App(False)

class Frame(wx.Frame):

    def Login(self, e):
        tmp = self.t_username.GetValue()
        print tmp

    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(400, 250))
        panel = wx.Panel(self)

        m_login = wx.Button(panel, wx.ID_OK, "Login", size=(100, 35), pos=(150,165))


        t_username = wx.TextCtrl(panel, -1, pos=(100, 50), size=(150, 30))
        m_login.Bind(wx.EVT_BUTTON, self.Login)

frame = Frame("Login Screen")
frame.Show()
app.MainLoop()

我试图将 Frame 类的名称,Bind 行改为

    self.Bind(wx.EVT_BUTTON, self.Login, m_login)

并删除自我。从tmp,但它没有工作。 感谢您的帮助。

【问题讨论】:

    标签: python python-2.7 wxpython


    【解决方案1】:

    您只需在 t_username 前面添加 self. 即可使 t_username 成为 Frame 类的属性。

    import wx
    app = wx.App(False)
    
    
    class Frame(wx.Frame):
    
        def Login(self, e):
            tmp = self.t_username.GetValue()
            print tmp
    
        def __init__(self, title):
            wx.Frame.__init__(self, None, title=title, size=(400, 250))
            panel = wx.Panel(self)
    
            m_login = wx.Button(
                panel, wx.ID_OK, "Login", size=(100, 35), pos=(150, 165))
    
            self.t_username = wx.TextCtrl(panel, -1, pos=(100, 50), size=(150, 30))
            m_login.Bind(wx.EVT_BUTTON, self.Login)
    
    frame = Frame("Login Screen")
    frame.Show()
    app.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 2016-05-26
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多