我不知道你是怎么运行的。我是说这样,打开PythonWin. 然后在里面用execfile的方式来运行这个脚本。第一次没问题, 第二次再执行execfile就会出问题。 当然如果关掉PythonWin,再重新打开,然后运行,当然也没问题
#coding=utf-8'''
Created on 2012-6-4
@author: Administrator
@note:
以下是具体代码:
我用的Python2.7
需要在当前文件夹下创建script.py
'''
import wx
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#修改位置,在读取的时候获取,在修改文件的时候使用
self.offsetx = 0
self.offsety = 0
self.initGUI()
def initGUI(self):
self.createWidgets()
self.setPrepertise()
self.doyout()
def createWidgets(self):
self.xshow_textCtrl = wx.TextCtrl(self, -1)
self.yshow_textCtrl = wx.TextCtrl(self, -1)
self.change_btn = wx.Button(self, -1, label = u"修改")
self.Bind(wx.EVT_BUTTON, self.onChange, self.change_btn)
def onChange(self, evt):
#获取修改的属性
x = self.xshow_textCtrl.GetLabelText()
y = self.yshow_textCtrl.GetLabelText()
fl = open("script.py", "w")
#找到相应位置修改
fl.seek(self.offsetx)
fl.write("x = %s\n" % x)
fl.seek(self.offsety)
fl.write("y = %s\n" % y)
fl.close()
def doyout(self):
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(mainSizer)
#x显示
xSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(xSizer, 1, wx.EXPAND)
x_label = wx.StaticText(self, -1, label = "X:")
xSizer.Add(x_label, 0, wx.ALL, 10)
xSizer.Add(self.xshow_textCtrl, 0, wx.ALL, 10)
#y显示
ySizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(ySizer, 1, wx.EXPAND)
y_label = wx.StaticText(self, -1, label = "Y:")
ySizer.Add(y_label, 0, wx.ALL, 10)
ySizer.Add(self.yshow_textCtrl, 0, wx.ALL, 10)
#按键
mainSizer.Add(self.change_btn, 0, wx.ALL, 10)
def setPrepertise(self):
x, y = self.getXY()
self.xshow_textCtrl.SetLabel(str(x))
self.yshow_textCtrl.SetLabel(str(y))
def getXY(self):
fl = open("script.py", "r")
msg = ""
line = fl.readline()
while line and line[-1]:
if line.startswith("x"):
msg += line
self.offsetx = fl.tell() - len(line)#得到x那行的起始位置
elif line.startswith("y"):
msg += line
self.offsety = fl.tell() - len(line)#得到y那行的起始位置
line = fl.readline()
x = 0
y = 0
exec msg#给x,y赋值
return x, y
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, title = u"测试Frame", size = (400, 300),
pos = (400, 200))
TestPanel(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()
#!/usr/bin/env python# -*- coding: utf-8 -*-
import wx
class MyFrame3(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame3.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.sizer_7_staticbox = wx.StaticBox(self, -1, _(u"显示区"))
self.sizer_6_staticbox = wx.StaticBox(self, -1, _(u"功能区"))
self.radio_box_1 = wx.RadioBox(self, -1, _("radio_box_1"), choices=[_("choice 1"), _("choice 2"), _("choice 3")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
self.button_1 = wx.Button(self, -1, _("button_1"))
self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_RICH2)
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.clickButton, self.button_1)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame3.__set_properties
self.SetTitle(_("frame_4"))
self.SetSize((400, 300))
self.radio_box_1.SetMinSize((210, 90))
self.radio_box_1.SetSelection(0)
self.button_1.SetMinSize((170, 90))
self.text_ctrl_1.SetMinSize((390, 99))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame3.__do_layout
sizer_4 = wx.BoxSizer(wx.VERTICAL)
sizer_5 = wx.BoxSizer(wx.VERTICAL)
sizer_7 = wx.StaticBoxSizer(self.sizer_7_staticbox, wx.VERTICAL)
sizer_6 = wx.StaticBoxSizer(self.sizer_6_staticbox, wx.VERTICAL)
sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
sizer_8.Add(self.radio_box_1, 0, wx.ALIGN_CENTER_VERTICAL, 0)
sizer_8.Add(self.button_1, 0, wx.ALIGN_CENTER_VERTICAL, 0)
sizer_6.Add(sizer_8, 1, wx.EXPAND, 0)
sizer_5.Add(sizer_6, 1, wx.EXPAND, 0)
sizer_7.Add(self.text_ctrl_1, 0, 0, 0)
sizer_5.Add(sizer_7, 1, wx.EXPAND, 0)
sizer_4.Add(sizer_5, 1, wx.EXPAND, 0)
self.SetSizer(sizer_4)
self.Layout()
# end wxGlade
def clickButton(self, event):
print "Event handler `clickButton' not implemented!"
event.Skip()
# end of class MyFrame3
if __name__ == "__main__":
import gettext
gettext.install("app") # replace with the appropriate catalog name
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_4 = MyFrame3(None, -1, "")
app.SetTopWindow(frame_4)
frame_4.Show()
app.MainLoop()
按照你的要求做了个界面,调用clickButton函数处理你的逻辑,然后调用textbox的setvalue方法将你的结果写到文本框中
你觉得界面不合理的话,你可以自己调用layout和properties两个参数是界面参数
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)