导入变量

2024-03-28 08:17:44 发布

您现在位置:Python中文网/ 问答频道 /正文

attrubuI我是python新手,但不知道如何解决这个问题:

import wx

class myclass(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Frame',size=(300,200))
        panel=wx.Panel(self)

        button=wx.Button(panel,label="click me",size=(120,60))
        self.Bind(wx.EVT_BUTTON, self.clickbutton, button)

        value=1

    def clickbutton(self, event):
        if self.value == 1:
            print("success")

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

当我点击按钮时,我收到这个错误:“AttributeError:'myclass'对象没有属性'value'”。我做错什么了?/如何将“值”导入“clickbutton”函数?你知道吗

编辑: 好吧,这个问题解决了。在那之后我想修改一下:

    self.dropdown=wx.Choice(panel,pos=(130,60))
    list = ['banana', 'apple', 'strawberry']
    self.dropdown.AppendItems(strings=list)

def clickbutton(self, event):
    if self.dropdown.GetStringSelection() == 'apple':
        print("success")

实际上,这是有效的。。。。但我花了很长时间才明白,我必须在最后一行中使用“GetStringSelection()”。如何显示具有可能属性的列表(如“GetStringSelection()”)”wx.选择“(和其他wx类)?还是有一个好的网站?你知道吗

编辑2: 再次感谢! 我还有最后一个问题(抱歉一步步问):

dir(wx.Choice)

工作,但我有一个给定的代码与obejct称为“VarDecl”。如果我问的话,我会得到一个错误

dir(wx.VarDecl)

我在为这个VarDecl对象寻找一个类似“HasChanged”的属性。你知道吗

Traceback (most recent call last):
File [...]    
if self.theVariable.HasChanged():
AttributeError: 'VarDecl' object has no attribute 'HasChanged'

编辑3: 好吧,我认为这和“wx”没有关系,我认为它来自“re”。(还是“wx”?我很困惑)


Tags: selfid编辑if属性valuedefmyclass
1条回答
网友
1楼 · 发布于 2024-03-28 08:17:44

value=1更改为self.value = 1。您现在拥有的value是一个局部变量,它在退出构造函数时被丢弃。你知道吗

编辑:至于第二个问题,内置函数dir(object)将为您提供给定对象范围内的名称(包括方法)列表。 More information available here

编辑2:The wxPython documentation may also be of use to you

编辑3:关于变量的事情:你试过dir(self.theVariable)吗?你知道吗

相关问题 更多 >