【问题标题】:Update imported variable from another file从另一个文件更新导入的变量
【发布时间】:2019-05-18 21:41:50
【问题描述】:
  1. 在文件 A 中,我从文件 B 中导入了变量“x”

  2. 我更改了文件 A 中“x”的值

  3. 然后我覆盖文件 B 的“x”声明。

现在我需要在文件 A 中使用文件 B 中的“x”的新值。但文件 A 并未更新为文件 B 获得的新值——它继续使用旧值(这不会t 不再存在)。如果我停止然后再次运行脚本,文件 A 只会重新调整更改。

使用的真实姓名:

  • 导入的变量 = 'vert' 和 'hor'。
  • 文件 B = 'blab'

源文件 A:

from blab import *


nm = 'nome'
while nm != 'proximo' :
    from blab import *  # here_point
    import blab
    from blab import vert
    from blab import hor

    nm = str(input('Cadastrar - nome : '))
    pos = str(input(' Cadastrar - : %s . Ocupação : ' %(nm)))
    grp = str(input(' Cadastrar - : %s . Grupo : ' %(nm)))
    pos = pos.replace(" ", "_")
    grp = grp.replace(" ", "_")
    nm = nm.replace(" ","_")
    if nm != 'proximo' :
        if vert == -340:
            new_vert = 350
            new_hor = hor + 200
        if vert!= -340:
            new_vert= vert-30
            new_hor = hor
        txt_vert = new_vert - 15
        txt_hor = new_hor + 20
        with open("blab.py", "a") as fd:
            fd.write("\nt%s = turtle.Turtle(shape='turtle')\nt%s.color('pink')\ntext%s = turtle\ntext(text%s, '%s', '20', 'green', %s, %s)\nt%s.goto(%s, %s)" %(nm,nm,nm,nm,nm,txt_hor,txt_vert,nm,new_hor,new_vert))
            fd.write("\ndef t%s_handler(x, y):\n    pnt = screen.textinput(' pontuação', '%s:  ')" %(nm,nm))
            fd.write("\n    pnt = [int(x) for x in pnt.split()]\n    if len(pnt) == 5 :\n        with open('%s.py' %(mes), 'a') as fd:\n            fd.write('%s.pontuacao(%i,%i,%i,%i,%i)' % (pnt[0],pnt[1],pnt[2],pnt[3],pnt[4]))\n" )
            fd.write("        %s.color('blue')\n"%(nm))
            fd.write("t%s.onclick(t%s_handler)\n"%(nm,nm))
        print(vert,new_vert,hor,new_hor)
        with open('blab.py', 'r') as file:
            filedata = file.read()
            filedata = filedata.replace('vert=%i' %(vert),'vert=%i'  %(new_vert))
            with open('blab.py', 'w') as file:
                file.write(filedata)
                file.close()
        with open('blab.py', 'r') as file:
            filedatas = file.read()
            filedatas = filedatas.replace('hor=%.i'%(hor),'hor=%.i'%(new_hor))
            with open('blab.py', 'w') as file:
                file.write(filedatas)
                file.close()

源文件 B:

import turtle
screen = turtle.Screen()

width = 1200
height = 1500
turtle.screensize(width, height)


def text(t,text, size, color, pos1, pos2):
    t.penup()
    t.goto(pos1, pos2)
    t.color(color)
    t.begin_fill()
    t.write(text, font=('Arial', size, 'normal'))
    t.end_fill()


vert=350
hor=-600

我尝试在第 7 行重新导入变量,但没有成功。

注意:要运行一次脚本,只需在提示符处输入3个随机名称

【问题讨论】:

    标签: python python-3.x import python-import turtle-graphics


    【解决方案1】:

    这是您的示例的简化版本,它使用 reload - 它可能适用于您的目的:

    from importlib import reload
    import blab
    
    nm = 'nome'
    
    while nm != 'proximo':
        nm = str(input('Cadastrar - nome: '))
    
        if nm != 'proximo':
            new_vert = blab.vert - 100
            new_hor = blab.hor + 100
    
        print(blab.vert, new_vert, blab.hor, new_hor)
    
        with open('blab.py') as file:
            filedata = file.read()
            filedata = filedata.replace('vert=%i' % blab.vert, 'vert=%i' % new_vert)
    
        with open('blab.py', 'w') as file:
            file.write(filedata)
    
        with open('blab.py') as file:
            filedatas = file.read()
            filedatas = filedatas.replace('hor=%i' % blab.hor, 'hor=%i' % new_hor)
    
        with open('blab.py', 'w') as file:
            file.write(filedatas)
    
        reload(blab)
    

    输出

    > tail -2 blab.py 
    vert=350
    hor=-600
    > python3 fileA.py
    Cadastrar - nome: Larry
    350 250 -600 -500
    Cadastrar - nome: Moe
    250 150 -500 -400
    Cadastrar - nome: Shemp
    150 50 -400 -300
    Cadastrar - nome: proximo
    50 50 -300 -300
    > !tail
    tail -2 blab.py
    vert=50
    hor=-300
    > 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-19
      • 2021-04-17
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多