我正在尝试学习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,但它没有工作。 谢谢你的帮助。
答案 0 :(得分:1)
您只需要在其前面添加 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()