wxPython,尝试从textctrl获取值时出错

wxPython, error when trying to get value from textctrl

我正在努力学习Python和wxPython;我正在制作一个简单的 window,它应该在控制台上打印 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()

我尝试将框架的名称class、绑定线更改为

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

并删除自我。来自 tmp,但它没有用。 感谢您的帮助。

你只需要在 t_username Frame class 前面添加 self. 属性即可。

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()