Tkinter获得价值然后销毁

时间:2020-08-20 13:21:06

标签: python tkinter

我正在尝试创建一个具有2个窗口的应用程序。仅在首次运行应用程序时才调用第一个窗口(A),然后打开窗口(B)。对于所有将来的事件,仅调用窗口(B)。这是下面的代码:

# Script Counter/Setup
def read_counter():
    if path.exists("counter.json"):
        return loads(open("counter.json", "r").read()) + 1
    else:
        info = tk.Tk()
        info.title("Setup")
        info.geometry("350x120")
        info.grid_columnconfigure((0, 2), weight = 1)

        count = tk.Label(info, text = "Previous Post Number")
        string = tk.StringVar()
        count_input = tk.Entry(info, textvariable = string)
        val = string.get()

        def destroy_get():
            val = int(count_input.get())
            info.quit()
            return val

        count_button = tk.Button(info, text = "Done!", command = destroy_get)

        tk.Label(info, text = "Setup", font='Helvetica 18 bold').grid(row = 0, column = 1, padx = 5, pady = 5)
        count.grid(row = 1, column = 0, padx = 5, pady = 5)
        count_input.grid(row = 1, column = 2, padx = 5, pady = 5)
        count_button.grid(row = 2, column = 1, padx = 5, pady = 5)
        info.mainloop()
        # info.destroy()
        return destroy_get()

def write_counter():
    with open("counter.json", "w") as f:
        f.write(dumps(counter))

counter = read_counter()
atexit.register(write_counter)

folders = ["to_post/", "not_posted/", "posted"]
for folder in folders:
    if not path.exists(folder):
        os.mkdir(folder)

print(os.getcwd())
# GUI
window = tk.Tk()
window.title("Confessions Bot")
window.geometry("600x350")
window.grid_columnconfigure((0,2), weight = 1)

label_tell_account = tk.Label(window, text = "Tellonym Account")
label_tell_password = tk.Label(window, text = "Tellonym Password")
label_ig_account = tk.Label(window, text = "Instagram Account")
label_ig_password = tk.Label(window, text = "Instagram Password")

tell_account = tk.Entry(window)
tell_password = tk.Entry(window)
ig_account = tk.Entry(window)
ig_password = tk.Entry(window)

image = ImageTk.PhotoImage(Image.open("logo.png"))
tk.Label(window, image = image).grid(row = 0, column = 1, padx = 10, pady = 10)
label_tell_account.grid(row = 1, column = 0)
tell_account.grid(row = 1, column = 2, padx = 10, pady = 10)
label_tell_password.grid(row = 2, column = 0, padx = 10, pady = 10)
tell_password.grid(row = 2, column = 2, padx = 10, pady = 10)
label_ig_account.grid(row = 3, column = 0, padx = 10, pady = 10)
ig_account.grid(row = 3, column = 2, padx = 10, pady = 10)
label_ig_password.grid(row = 4, column = 0, padx = 10, pady = 10)
ig_password.grid(row = 4, column = 2, padx = 10, pady = 10)
# run.grid(row = 5, column = 1, padx = 10, pady = 10)

window.mainloop()

运行此命令后,我得到_tkinter.TclError: image "pyimage1" doesn't exist。我读到发生这种情况是因为我还没有破坏我的初始窗口。如果我将destroy_get()更改为info.destroy(),则无法再从窗口A中获取条目。

这是我第一次使用Tkinter进行编码,因此欢迎您提供帮助。

0 个答案:

没有答案