因此,我使用tkinter创建了一种计算器,并进行了快速设置,从而在AppData目录中创建了一个save.dat文件,该文件可作为python脚本使用,但是使用该行编译时:
pyinstaller --onefile -w file.py
不起作用。在exe
运行安装过程就好了,但现在看来,一些测试后,当它到达if语句的结束,如果save.dat已经存在(出现一个对话框,并有一个不再显示复选框和一个OK按钮),它不等待响应并关闭。当我检查taskmanager时,我可以一窥它作为后台进程,但是没有窗口出现过。我不了解问题所在,因此可以寻求帮助。这是我的代码片段:
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":
条件中调用,这将导致无限递归。对于您要实现的目标,我仍然不太清楚,请告诉我我的代码是否满足您的要求。希望能有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句