我需要将python GUI中的2个滑块的值发送到外部文件(只是文本或csv文件)。任何人都可以帮忙吗? 干杯 麦金太尔
答案 0 :(得分: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()