Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在执行时永久更改程序数据?_Python_User Interface_Python 3.x_Tkinter_Tk - Fatal编程技术网

Python 如何在执行时永久更改程序数据?

Python 如何在执行时永久更改程序数据?,python,user-interface,python-3.x,tkinter,tk,Python,User Interface,Python 3.x,Tkinter,Tk,我已经编写了一个小程序,它允许我在画布小部件中移动一个矩形。我计划扩展这个程序,允许我移动屏幕上的其他小部件,这样我就可以更容易地在窗口中移动东西,即通过拖放来移动项目,而不是指定直接坐标(至少不明确) 以下是迄今为止的代码: 从tkinter进口* root=Tk() 类移动\u形状: 数据={'x':0,'y':0} canvas=canvas(width=root.winfo\u screenwidth(),height=root.winfo\u screenheight()) 定义初始值

我已经编写了一个小程序,它允许我在画布小部件中移动一个矩形。我计划扩展这个程序,允许我移动屏幕上的其他小部件,这样我就可以更容易地在窗口中移动东西,即通过拖放来移动项目,而不是指定直接坐标(至少不明确)

以下是迄今为止的代码:

从tkinter进口* root=Tk()

类移动\u形状:
数据={'x':0,'y':0}
canvas=canvas(width=root.winfo\u screenwidth(),height=root.winfo\u screenheight())
定义初始值(自身、形状、填充='白色',*坐标):
shape\u coords=open('shape\u coords.py','r')
new_coords=shape_coords.readline()[1:-1]
新坐标=(新坐标).split(','))
如果是coords!=():新坐标=坐标
如果形状=‘线’:
标记='line'
self.canvas.create_line(坐标,标记=标记,填充=填充)
elif形状==“矩形”:
标记='rect'
self.canvas.create_矩形(坐标,tags=tag,fill=fill)
... 更多代码
self.canvas.tag\u绑定(标记“”,self.click)
self.canvas.tag_绑定(tag',self.track)
self.canvas.tag_绑定(tag',self.release)
self.canvas.grid()
def单击(自身,事件):
self.data.update({'x':event.x,'y':event.y})
self.item=self.canvas.find_最近(self.data['x'],self.data['y']))
def轨迹(自身、事件):
x、 y=event.x-self.data['x'],event.y-self.data['y']
self.canvas.move(self.item,x,y)
self.data.update({'x':event.x,'y':event.y})
def释放(自身、事件):
self.data.update({'x':event.x,'y':event.y})
shape\u coords=open('shape\u coords.py','w')
coords=str(self.canvas.coords(self.item))
形状协调。写入(协调)
shape_coords.close()
标签=标签(文本='文本更多文本更多文本',前景='白色',背景='白色')
移动形状(“矩形”、“蓝色”、50、50、200、200)
移动U形('椭圆形','绿色',50,50200200)
移动U形(‘圆弧’、‘红色’、50,50200200)
mainloop()
现在我可以在屏幕上的任何地方移动矩形。然而,当我重新打开窗口时,矩形从它的初始位置开始,尽管这是预期的结果。但是,当它从拖动的地方被拖放时,它能够保持矩形的位置,这是期望的结果

一个这样的解决方案,我可以考虑将其写入(并重写)另一个文件,这样新的coords就可以重写文件中的原始coords,尽管对我来说这看起来很混乱,因为我必须创建一个全新的文件。有没有更干净、更优雅的方法来实现这一点,或者我应该考虑将新的coords附加到一个文件中


提前感谢您的帮助

您需要决定的关键问题是:这些数据的范围是什么

是:

  • 属于程序本身的数据
  • 属于当前机器的数据
  • 属于当前用户的数据
  • 还有别的吗
如果数据属于(比如)当前用户,则修改程序本身是没有意义的


很可能,将数据写入其他地方的文件(或可能写入数据库)是更好的解决方案。

Hi,它是属于程序的数据,就像数据所属的实际文件一样(最上面)。它的标题是:)@Jim Jam我想你可以在同一个文件夹中写一个配置文件。你可以把它和脚本一起复制。@JimJam:如果我的回答不清楚,很抱歉。问题是,这些数据实际上属于程序吗?在思想上区分(1)程序的源代码,(2)安装在特定机器上的程序实例,以及(3)该程序的特定执行,这一点很重要。我认为用户可修改的数据永远不属于#1,而只属于#2和#3。因此,修改#1(程序的源代码)是不正确的,因为它在错误的抽象层次上解决了问题。你好,丹尼尔,我非常感谢你花时间回答我的问题,虽然我不清楚你的意思,但这是因为我缺乏经验(因为我在这方面才做了4个月),虽然我肯定会研究你提到的计划的各个方面。我不知道这是否澄清了问题,但我的意思是,在我将一个形状拖到某个地方之后,我试图更改变量coords。最后,我采用了将数据写入文件,然后读取数据的方法,这似乎是可行的。无论如何,再次感谢您抽出时间:)
class Move_Shape:
    data = {'x': 0, 'y': 0}
    canvas = Canvas(width = root.winfo_screenwidth(), height = root.winfo_screenheight())


    def __init__(self, shape, fill = 'White', *coords):
        shape_coords = open('Shape_coords.py', 'r')
        new_coords = shape_coords.readline()[1:-1]
        new_coords = (new_coords).split(',')

        if coords != (): new_coords = coords

        if shape == 'line': 
            tag = 'line'
            self.canvas.create_line(coords, tags = tag, fill = fill)

        elif shape == 'rectangle': 
            tag = 'rect'
            self.canvas.create_rectangle(coords, tags = tag, fill = fill)
        ... More Code


        self.canvas.tag_bind(tag, '<Button-1>', self.click)
        self.canvas.tag_bind(tag, '<Button1-Motion>', self.track)
        self.canvas.tag_bind(tag, '<ButtonRelease-1>', self.release)
        self.canvas.grid()


    def click(self, event):
        self.data.update({'x': event.x, 'y': event.y})
        self.item = self.canvas.find_closest(self.data['x'], self.data['y'])

    def track(self, event):
        x, y = event.x - self.data['x'], event.y - self.data['y']
        self.canvas.move(self.item, x, y)
        self.data.update({'x': event.x, 'y': event.y})

    def release(self, event):
        self.data.update({'x': event.x, 'y': event.y})
        shape_coords = open('shape_coords.py', 'w')
        coords = str(self.canvas.coords(self.item))
        shape_coords.write(coords)
        shape_coords.close()

label = Label(text = 'text more text some more text', fg = 'white', bg = 'white')

Move_Shape('rectangle', 'blue', 50, 50, 200, 200)
Move_Shape( 'oval', 'green', 50,50,200,200)
Move_Shape( 'arc', 'red', 50,50,200,200)



mainloop()