Send value from python GUI to an external file
I need to have values from 2 sliders in a pytho开发者_C百科n GUI sent to an external file (just a text or csv file).Can anyone help? Cheers Alasdair
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()
精彩评论