简体   繁体   中英

How do i get information from a slider using button in wxpython?

I get the following error AttributeError: 'Button' object has no attribute 'GetValue' I'm running Python 3.9.13

import wx
import os
style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER
class main(wx.Frame) :
    def __init__(self,parent,id) :
        wx.Frame.__init__(self,parent,id,"Slider", size=(500,300),style=style)
        panel=wx.Panel(self)
        button=wx.Button(panel,label="Work!", pos=(90,90), size=(100,100))
        slider= wx.Slider(panel, value=1, minValue=1, maxValue=255, style= wx.SL_HORIZONTAL | wx.SL_LABELS, pos=(200,90), size=(250,60))
        self.Bind(wx.EVT_BUTTON, self.val, button,slider)
        self.Bind(wx.EVT_CLOSE, self.closewindow)
        self.Center() 
    def val(self,event):
        obj = event.GetEventObject() 
        val = obj.GetValue() 
        print(val)
    def closewindow(self,windows):
        self.Destroy()



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

I fixed it! You just need to add glboal value!

#!/usr/bin/python
from logging.config import valid_ident
import wx
import os
global val
val = 1
style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER
class main(wx.Frame) :
    def __init__(self,parent,id) :
        wx.Frame.__init__(self,parent,id,"Slider", size=(500,300),style=style)
        panel=wx.Panel(self)
        button=wx.Button(panel,label="Work!", pos=(90,90), size=(100,100))
        slider= wx.Slider(panel, value=1, minValue=1, maxValue=255, style= wx.SL_HORIZONTAL | wx.SL_LABELS, pos=(200,90), size=(250,60))
        self.Bind(wx.EVT_BUTTON, button,)
        self.Bind(wx.EVT_SLIDER, slider, self.val)
        self.Bind(wx.EVT_CLOSE, self.closewindow)
        self.Center() 
    def val(self,event):
        global val
        obj = event.GetEventObject() 
        val = obj.GetValue() 
        print(val)
    def closewindow(self,windows):
        self.Destroy()



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

Rather than dip into using globals , there are easier ways to get what you want, by binding to widgets that are declared as instance variables ie self.something and then binding to that. You are, after all declaring self in each def , before the event .
In this way, you do not have to quiz the event object, instead you can directly access the widget by name.

eg (Your code amended slightly)

#!/usr/bin/python
import wx

style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER

class main(wx.Frame) :
    def __init__(self,parent,id) :
        wx.Frame.__init__(self,parent,id,"Slider", size=(500,300),style=style)
        panel=wx.Panel(self)
        self.button=wx.Button(panel,label="Work!", pos=(90,90), size=(100,100))
        self.slider= wx.Slider(panel, value=1, minValue=1, maxValue=255, style= wx.SL_HORIZONTAL | wx.SL_LABELS, pos=(200,90), size=(250,60))
        self.button.Bind(wx.EVT_BUTTON, self.Onbutton,)
        self.Bind(wx.EVT_CLOSE, self.closewindow)
        self.Center() 

    def Onbutton(self,event):
        print("Button Pressed")
        val = self.slider.GetValue() 
        print("Slider value is : ", val)

    def closewindow(self,windows):
        self.Destroy()

if __name__=='__main__':
    app=wx.App()
    frame=main(parent=None,id=1)
    frame.Show()
    app.MainLoop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM