2010-12-05 27 views
0

我需要將來自2個滑塊的值發送到外部文件(只是一個文本或csv文件)。任何人都可以幫忙嗎? 乾杯 麥金太爾將python GUI的值發送到外部文件

+0

你有什麼迄今所做? – 2010-12-05 18:08:09

+1

順便說一下,什麼是python GUI? – khachik 2010-12-05 18:10:12

回答

2

這裏有一個簡單的例子使用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() 
相關問題