在第二个窗口中保存用户输入

发布于 2025-01-15 04:44:15 字数 1020 浏览 4 评论 0原文

我的代码有问题,当我运行程序时而不是单击按钮时保存用户输入我还尝试创建一个函数来检测是否entry1.get() == none不保存输入“输入“但这不起作用,感谢您的帮助并为

我的代码计时:

#imports

#define the first tk window
window = Tk()
window.geometry("655x600")
window.title("")

icon = PhotoImage(file="data/icons/icon.png")
window.iconphoto(True,icon)
window.config(background="#2e3033")

#savefiles
savefile1 = open("data/userinput/data1.txt","w", encoding="utf-8")

#button  functions
def new_window1():
    global entry1
    window2 = Tk()
    window2.geometry("500x100+200+300")
    window2.config(background="#2e3033")
    window2.title("Edit Button 1")
    entry1 = Entry(window2,width=100, font= ("Arial",12))
    entry1.place(x=5,y=30)
    Button1 = Button(window2, text="save",command=lambda:[savefile1.write(entry1.get()),window2.destroy()])
    Button1.place(x=5,y=70)


#buttons
image1 = PhotoImage(file="images/streamdeximage1.png")
button1 = Button(window, text="hello" , command=new_window1 , image=image1)
button1.place(x=20,y=20)

window.mainloop()

I have a problem with my code the user input is saved when I run the program instead of when the button is clicked I also tried to make a function that detects if entry1.get() == none don't save the input "input" but this did not work thanks for your help and time

my code:

#imports

#define the first tk window
window = Tk()
window.geometry("655x600")
window.title("")

icon = PhotoImage(file="data/icons/icon.png")
window.iconphoto(True,icon)
window.config(background="#2e3033")

#savefiles
savefile1 = open("data/userinput/data1.txt","w", encoding="utf-8")

#button  functions
def new_window1():
    global entry1
    window2 = Tk()
    window2.geometry("500x100+200+300")
    window2.config(background="#2e3033")
    window2.title("Edit Button 1")
    entry1 = Entry(window2,width=100, font= ("Arial",12))
    entry1.place(x=5,y=30)
    Button1 = Button(window2, text="save",command=lambda:[savefile1.write(entry1.get()),window2.destroy()])
    Button1.place(x=5,y=70)


#buttons
image1 = PhotoImage(file="images/streamdeximage1.png")
button1 = Button(window, text="hello" , command=new_window1 , image=image1)
button1.place(x=20,y=20)

window.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2025-01-22 04:44:15

即使你的代码保存了我这边第二个窗口的按钮单击时的数据,这里有一些更改

  • 应该只有一个根窗口,即 TK(),如果您有多个窗口,则使用 Toplevel()
  • 使用上下文用于在打开文件但从未关闭时将数据保存在文件中的管理器
  • 删除该全局条目声明,这没有任何意义
  • 如果需要通过刷新缓冲区立即写入数据,请使用 file.flush()
def save_data(data):
    with open("data.txt", "w", encoding="utf-8") as file:
        file.write(data)
        file.flush()


# button  functions
def new_window1():
    window2 = Toplevel()
    window2.geometry("500x100+200+300")
    window2.config(background="#2e3033")
    window2.title("Edit Button 1")
    entry1 = Entry(window2, width=100, font=("Arial", 12))
    entry1.place(x=5, y=30)
    button1 = Button(window2, text="save", command=lambda: [save_data(entry1.get()), window2.destroy()])
    button1.place(x=5, y=70)

Even though ur code saves the data on button click of second window on my side, here are some changes

  • There should be only one root window, that's TK(), if you have more than one window, then use Toplevel()
  • Use a context manager for saving data in the file as you opened the file but never closed it
  • remove that global declaration of entry, that doesn't make any sense
  • Use file.flush() if you need to write data immediately by flushing the buffer
def save_data(data):
    with open("data.txt", "w", encoding="utf-8") as file:
        file.write(data)
        file.flush()


# button  functions
def new_window1():
    window2 = Toplevel()
    window2.geometry("500x100+200+300")
    window2.config(background="#2e3033")
    window2.title("Edit Button 1")
    entry1 = Entry(window2, width=100, font=("Arial", 12))
    entry1.place(x=5, y=30)
    button1 = Button(window2, text="save", command=lambda: [save_data(entry1.get()), window2.destroy()])
    button1.place(x=5, y=70)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文