【问题标题】:How do you permanently alter program data upon execution?如何在执行时永久更改程序数据?
【发布时间】:2015-04-24 00:19:49
【问题描述】:

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

这是目前为止的代码:

从 tkinter 导入 * root = Tk()

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()

所以现在我可以将矩形移动到屏幕上的任何位置。但是,当我重新打开窗口时,矩形从它的初始位置开始,尽管这是预期的结果。尽管它能够将矩形从被拖动的位置放下时保持在其所在的位置,但仍是理想的结果。

一个这样的解决方案我可以想到它写入(和重写)到另一个文件,以便新的坐标重写文件中的原始坐标,尽管对我来说这似乎很混乱,因为我必须创建一个全新的文件.是否有更清洁、更优雅的方式来实现这一点,还是我应该考虑将新坐标附加到文件中?

提前感谢您的帮助!

【问题讨论】:

    标签: python user-interface python-3.x tkinter tk


    【解决方案1】:

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

    是吗:

    • 属于程序本身的数据
    • 属于当前机器的数据
    • 属于当前用户的数据
    • 还有别的吗?

    如果数据属于当前用户,那么修改程序本身是没有意义的。

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

    【讨论】:

    • 嗨,这是属于程序的数据,就像在数据所属的实际文件中一样(在顶部)。它在标题中:)
    • @Jim Jam 我猜你可以在同一个文件夹中编写一个配置文件。您可以将其与脚本一起复制。
    • @JimJam:抱歉,如果我的回答不清楚。关键是,数据真的属于程序吗?在心理上区分 (1) 程序的源代码、(2) 安装在特定机器上的该程序的实例以及 (3) 该程序的特定执行是很重要的。我认为用户可修改的数据从不属于#1,只有#2 和#3。因此,修改 #1(程序的源代码)是不正确的,因为它在错误的抽象级别上解决了问题。
    • 您好 Daniel,非常感谢您抽出宝贵时间回答我的问题,虽然我不清楚您的意思,但这是由于我缺乏经验(因为我只是在这 4 个月),虽然我肯定会研究你提到的程序的所有方面。我不知道这是否能澄清事情,但我的意思是我试图改变变量坐标,在我把一个形状拖到某个地方之后。最后,我采用了将数据写入文件然后读回数据的方法,这似乎可行。无论如何,再次感谢您的时间:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2011-07-27
    • 2014-08-14
    相关资源
    最近更新 更多