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
-
1What is python GUI, by the way?khachik– khachik2010-12-05 18:10:12 +00:00Commented Dec 5, 2010 at 18:10
-
1Short answer: When the value is available, open the file, write the value, and then close it. Does that not work for some reason?martineau– martineau2010-12-05 18:35:28 +00:00Commented Dec 5, 2010 at 18:35
Add a comment
|
1 Answer
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()
2 Comments
khachik
@K. Brafford It writes to the file on each slide event?
K. Brafford
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.