wxPython:对象没有属性错误

发布于 2024-11-19 09:23:38 字数 1757 浏览 2 评论 0原文

自学 Python 和 wxPython。不确定我在这里做错了什么,可以使用比我聪明得多的人的一些见解...

import wx
from wxPython.wx import *

class myGUI(wx.Frame):

    def __init__(self, parent, title):    
        super(myGUI, self).__init__(parent, title=title,
            size=(450, 350))
        panel = wx.Panel(self)
        sizer = wx.GridBagSizer(5, 5)

    # Main Database Text, Entry and Browse Button ------------------------------
        label_MainDatabase = wx.StaticText(panel, label="Main Database:")
        sizer.Add(label_MainDatabase, pos=(0, 0), flag=wx.LEFT|
            wx.ALIGN_CENTER_VERTICAL, border=20)

        tc_MainDatabase = wx.TextCtrl(panel)
        sizer.Add(tc_MainDatabase, pos=(0, 1), span=(1, 3), flag=wx.TOP|
            wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)
        tc_MainDatabase.Value = "DBG: I am properly initialized."

        bt_MainDatabase = wx.Button(panel, label="Browse...")
        sizer.Add(bt_MainDatabase, pos=(0, 4), flag=wx.LEFT|wx.RIGHT|
        wx.ALIGN_CENTER_VERTICAL, border=0)
        bt_MainDatabase.Bind(wx.EVT_BUTTON, self.bt_MainDatabaseClick,
            bt_MainDatabase)
    # --------------------------------------------------------------------------
        sizer.AddGrowableCol(2)
        panel.SetSizer(sizer)

        self.Centre()
        self.Show()

    def bt_MainDatabaseClick(self, event):
        # Create a list of filters
        self.tc_MainDatabase.SetValue = "A"


if __name__ == '__main__':
    app = wx.App()
    myGUI(None, title="myGUI")

    app.MainLoop()

当我单击“浏览”按钮时,出现以下错误: AttributeError:“myGUI”对象没有属性“tc_MainDatabase”

我做错了什么?我试图从“浏览”按钮捕获信息,然后更新文本控制字段(tc_MainDatabase)。我尝试过重新排列 def 语句的顺序等。

是的,我总是用双脚跳进去。这是我知道如何学习的唯一方法...:)

谢谢。

-周先生

teaching myself Python and wxPython. Not sure what I am doing wrong here, could use some insight from folks much smarter than myself...

import wx
from wxPython.wx import *

class myGUI(wx.Frame):

    def __init__(self, parent, title):    
        super(myGUI, self).__init__(parent, title=title,
            size=(450, 350))
        panel = wx.Panel(self)
        sizer = wx.GridBagSizer(5, 5)

    # Main Database Text, Entry and Browse Button ------------------------------
        label_MainDatabase = wx.StaticText(panel, label="Main Database:")
        sizer.Add(label_MainDatabase, pos=(0, 0), flag=wx.LEFT|
            wx.ALIGN_CENTER_VERTICAL, border=20)

        tc_MainDatabase = wx.TextCtrl(panel)
        sizer.Add(tc_MainDatabase, pos=(0, 1), span=(1, 3), flag=wx.TOP|
            wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)
        tc_MainDatabase.Value = "DBG: I am properly initialized."

        bt_MainDatabase = wx.Button(panel, label="Browse...")
        sizer.Add(bt_MainDatabase, pos=(0, 4), flag=wx.LEFT|wx.RIGHT|
        wx.ALIGN_CENTER_VERTICAL, border=0)
        bt_MainDatabase.Bind(wx.EVT_BUTTON, self.bt_MainDatabaseClick,
            bt_MainDatabase)
    # --------------------------------------------------------------------------
        sizer.AddGrowableCol(2)
        panel.SetSizer(sizer)

        self.Centre()
        self.Show()

    def bt_MainDatabaseClick(self, event):
        # Create a list of filters
        self.tc_MainDatabase.SetValue = "A"


if __name__ == '__main__':
    app = wx.App()
    myGUI(None, title="myGUI")

    app.MainLoop()

I get the following error when I click the "Browse" button:
AttributeError: 'myGUI' object has no attribute 'tc_MainDatabase'

What am I doing wrong? I am trying to capture information from the Browse button and then update a text control field (tc_MainDatabase). I've tried rearranging the order of the def statements, etc.

And yes, I always jump in with both feet. It's the only way I know how to learn... :)

Thanks.

-Chow

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

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

发布评论

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

评论(1

巴黎夜雨 2024-11-26 09:23:38

也许是因为你说:

tc_MainDatabase = wx.TextCtrl(panel)

而不是:

self.tc_MainDatabase = wx.TextCtrl(panel)

Maybe it's because you're saying:

tc_MainDatabase = wx.TextCtrl(panel)

instead of:

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