0

I need to have values from 2 sliders in a python GUI sent to an external file (just a text or csv file).Can anyone help? Cheers Alasdair

2
  • 1
    What is python GUI, by the way? Commented Dec 5, 2010 at 18:10
  • 1
    Short answer: When the value is available, open the file, write the value, and then close it. Does that not work for some reason? Commented Dec 5, 2010 at 18:35

1 Answer 1

2

Here's a quick example using wx.Python:

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent, id = -1):
        wx.Panel.__init__(self, parent, id)

        self.slider1 = wx.Slider(self, -1, 50, 0, 100, size=(300,25))
        self.slider2 = wx.Slider(self, -1, 50, 0, 100, size=(300,25))

        self.button = wx.Button(self, -1, "Write Values")
        self.Bind(wx.EVT_BUTTON, self.onWrite)

        #  Uncomment the next two lines if you want to write the
        #  data out every time you move the slider
        #self.Bind(wx.EVT_SLIDER, self.onWrite)
        #self.onWrite()

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.AddStretchSpacer(1)
        self.sizer.Add(self.slider1, 0, wx.ALIGN_CENTER_HORIZONTAL)
        self.sizer.AddSpacer(50)
        self.sizer.Add(self.slider2, 0, wx.ALIGN_CENTER_HORIZONTAL)
        self.sizer.AddSpacer(75)
        self.sizer.Add(self.button, 0, wx.ALIGN_CENTER_HORIZONTAL)
        self.sizer.AddStretchSpacer(1)
        self.SetSizerAndFit(self.sizer)

    def onWrite(self, event = None):
        v1 = self.slider1.GetValue()
        v2 = self.slider2.GetValue()
        f = open("file.csv", "w")
        line = "%d, %d\n" %(v1, v2)
        f.write(line)
        f.close()
        print "Just wrote", line


if __name__ == "__main__":
    a = wx.PySimpleApp()
    f = wx.Frame(None,-1, "Slider Demo")
    p = MyPanel(f)
    f.Show()
    a.MainLoop()
Sign up to request clarification or add additional context in comments.

2 Comments

@K. Brafford It writes to the file on each slide event?
Good point. I wasn't sure based on the question, so I figured I'd show how to get notified of the slider changes. I modded the example to write deliberately via button press.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.