gpt4 book ai didi

Python tkinter - 编译成功但程序在编译为 Exe 时运行不正确

转载 作者:行者123 更新时间:2023-12-04 08:15:17 24 4
gpt4 key购买 nike

因此,我使用 tkinter 创建了一种计算器,并使其快速设置在 AppData 目录中创建了一个 save.dat 文件,它可以作为 python 脚本工作,但是当使用以下行编译时:pyinstaller --onefile -w file.py这是行不通的。 exe运行安装过程就好了,但似乎经过一些测试,当它到达 if 语句的末尾时,如果 save.dat 已经存在(出现一个对话框,并且有一个不再显示复选框和一个确定按钮) ,它不会等待响应并关闭。当我检查任务管理器时,我瞥见了它作为后台进程,但没有出现任何窗口。我不明白问题是什么,所以我可以使用一些帮助。这是我的代码片段:

from tkinter import *
from tkinter import messagebox
import pickle
import os.path
import sys

def saving():
messagebox.showinfo("Setting Up", "Setup will now begin")
path = os.path.expanduser("~/AppData/Local")
os.chdir(path)
os.makedirs("hydrocalc")
loc = os.path.expanduser("~/AppData/Local/hydrocalc/save.dat")
new = "0"
pickle.dump(new, open(loc, "wb"))

popup = Tk()
popup.withdraw()
loc = os.path.expanduser("~/AppData/Local/hydrocalc/save.dat")
if (os.path.exists(loc)):
i = pickle.load(open(loc, "rb"))
# then a few variables referring to the calculator
def world():
#the functions referring to the calculator
if (i == "0"):
popup.deiconify()
popup["bg"] = "#f0f0f0"
popup.title("INSTRUCTIONS")
labelpu = Label(popup, bg="white", text= #instructions on usage, justify="left").grid()
popup.resizable(width=False, height=False)
var = StringVar()
check = Checkbutton(popup, text="Don't Show Again", variable=var, onvalue="1", offvalue="0",
bg="#f0f0f0")
check.deselect()
check.grid()
lbl = Label(popup, text=var.get())

def btnOkay():
global i
lbl = Label(popup, text=var.get())
if (var.get() == "0"):
popup.withdraw()
i = "1"
calc.deiconify()
world()

elif (var.get() == "1"):

info = open(loc, 'w+')
new = "1"
pickle.dump(new, open(loc, "wb"))
popup.withdraw()
calc.deiconify()
i = "1"
world()
popup.deiconify()
btnOK = Button(popup, text="OK", bg="#f0f0f0", justify="center", width=20,
command=lambda:btnOkay()).grid()
elif (i == "1"):
calc.deiconify()
world()
else:
saving()
messagebox.showinfo("Setup Complete", "Setup is now complete. Please restart the program to
continue.")
sys.exit()

最佳答案

有一些更改要进行,请参阅下面的更新代码

from tkinter import *
from tkinter import messagebox
import pickle
import os.path
import sys

def saving():
messagebox.showinfo("Setting Up", "Setup will now begin")
path = os.path.expanduser(appdata)
os.chdir(path)
os.makedirs(appdir)
loc = os.path.expanduser(pref_file)
new = "0"
pickle.dump(new, open(loc, "wb"))

popup = Tk()
popup.withdraw()

appdata=os.getenv('APPDATA') #modified for my convenience
appdir=os.path.join(appdata,'MyApp')
pref_file=os.path.join(appdir,'pref.pickle')
loc = os.path.expanduser(pref_file)

if (os.path.exists(loc)):

def world():
#the functions referring to the calculator
if (i == "0"):
popup.deiconify()
popup["bg"] = "#f0f0f0"
popup.title("INSTRUCTIONS")
labelpu = Label(popup, bg="white", text='instructions on usage', justify="left").grid()
popup.resizable(width=False, height=False)
var = StringVar()
check = Checkbutton(popup, text="Don't Show Again", variable=var, onvalue="1", offvalue="0",
bg="#f0f0f0")
check.deselect()
check.grid()
lbl = Label(popup, text=var.get())
btnOK = Button(popup, text="OK", bg="#f0f0f0", justify="center", width=20,command=lambda:btnOkay()).grid()
def btnOkay():
global i
lbl = Label(popup, text=var.get())
if (var.get() == "0"):
popup.withdraw()
i = "1"
#calc.deiconify()
print('deiconified calc') #added for debugging

elif (var.get() == "1"):

info = open(loc, 'w+')
new = "1"
pickle.dump(new, open(loc, "wb"))
popup.withdraw()
#calc.deiconify()
print('deiconified calc') #added for debugging
i = "1"
world()
popup.deiconify()

elif i == "1":
#calc.deiconify() commented for debugging
print('deiconified calc')
exit() #added for debugging

i = pickle.load(open(loc, "rb"))
world()

else:
saving()
messagebox.showinfo("Setup Complete", "Setup is now complete. Please restart the program to continue.")
sys.exit()

popup.mainloop()
备注
  • 您没有 mainloop()对于您的程序,因此您的窗口将永远不会出现。
  • 没有初始调用 world() if 中的函数健康)状况。
  • 不要打电话world()来自 elif i == "1":条件,它会导致无限递归。

  • 我仍然不完全清楚您到底想实现什么,让我知道我的代码是否满足您的要求。希望它有所帮助。

    关于Python tkinter - 编译成功但程序在编译为 Exe 时运行不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65735964/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com