有一个功能OpenWin1()
可以打开一个带有设置的窗口,您可以在其中输入坐标、尺寸等(到目前为止我只完成了坐标)。
单击时,会在另一个文件中创建一行包含设置的内容。
一切正常,一切都很好,但变量没有更新,采用默认文本(pos Y
, pos X
)。
我们需要确保当按下按钮时,"Update"
变量会被更新。
import tkinter as tk
from tkinter import ttk
import subprocess
import time
from tkinter import *
root = Tk()
root.title('123')
root.geometry('400x600')
root.resizable(width=False, height=False)
font = ('intro.otf', 12)
def Insert(file_name, text_line, text_to_add):
with open(file_name, 'r', encoding='utf-8') as read_file:
text = read_file.readlines()
text.insert(text_line, text_to_add+"\n")
with open(file_name, 'w', encoding='utf-8') as write_file:
for i in text:
write_file.write(i)
with open(r"game.py", 'r') as fp:
for count, line in enumerate(fp):
pass
def OpenWin1():
Win1 = Tk()
Win1.title('Setup gameobject')
Win1.geometry('220x200')
Win1.resizable(width=False,height=False)
WB1 = Button(Win1,text='Add gameobject',command=lambda:Insert("game.py", 24, f' pygame.draw.rect(screen, WHITE, ({Xprint}, {Yprint}, 75, 75))'), font=font)
WB1.place(y=10,x=40)
#Yprint
info_Tf = Entry(Win1)
info_Tf.insert(END, 'Pos Y')
info_Tf.place(y=120,x=40)
#Xprint
info_Tf2 = Entry(Win1)
info_Tf2.insert(END, 'Pos X')
info_Tf2.place(y=100,x=40)
Yprint = info_Tf.get()
Xprint = info_Tf2.get()
Win1.mainloop()
btn1 = Button(root, text='Add Rect(Game Object)', command=lambda:OpenWin1())
btn1.place(x=0, y=0)
root.mainloop()
我需要将输入字段中写入的内容写入变量。出现这种情况,但是并没有更新,而是一开始就只写了一次。
重要的!如果要运行此代码,请在与此代码相同的文件夹中创建一个game.py文件。
作为一个选项: