0

So I've been having a weird bug with Python and Tkinter where a message box pops up too early. I don't know what's wrong: it should be working, right?

from tkinter import *
from tkinter import messagebox

with open("passwords.passwords", "a+") as f:
    root = Tk()
    root.geometry("1000x1000")
    root.wm_title("Info Collect")

    mylist = []

    var1 = StringVar()
    var1.set("Application:")
    label1 = Label(root, textvariable=var1, height=2)
    label1.grid(row=0, column=0)
    var2 = StringVar()
    var2.set("Password:")
    label2 = Label(root, textvariable=var2, height=2)
    label2.grid(row=0, column=3)

    ID1 = StringVar()
    ID2 = StringVar()
    box1 = Entry(root, bd=4, textvariable=ID1)
    box1.grid(row=0, column=1)
    box2 = Entry(root, bd=5, textvariable=ID2)
    box2.grid(row=0, column=4)


    def get_info():
        f.write("{0}: {1} ".format(box1.get(), box2.get()))

    def output_info():
        messagebox.showinfo(f.read())

    buttonA = Button(root, text="Save info", command=get_info, width=8)
    buttonA.grid(row=0, column=2)
    buttonB = Button(root, text="Output info", command=output_info(), width=8)
    buttonB.grid(row=0, column=5)

    root.mainloop()

That is all the code, do I have to do anything to it?

1 Answers1

0

Change this

buttonB = Button(root, text="Output info", command=output_info(), width=8)

to

buttonB = Button(root, text="Output info", command=output_info, width=8)

with parenthesis, the function is called and the returned value will be set to command, when you remove parenthesis you pass the function itself to command to be called when the button is clicked

Mohd
  • 5,523
  • 7
  • 19
  • 30
  • Thanks, I didn't even know I put those there. –  Jun 12 '20 at 00:18
  • Actually if you can help me again it won't read the file under messagebox.showinfo(f.read) where it returns empty. –  Jun 12 '20 at 00:22
  • because when you open the file with `a+` mode you might need to `f.seek(0)` to read from start, I suggest you take your code out of `with open()` and open the file only for reading/writing in the functions – Mohd Jun 12 '20 at 00:28
  • Thank you again! Another error on my end where I was doing f.read() as the title of the popup! –  Jun 12 '20 at 00:29