读取一些线程有助于我从tkinter gui创建一个.exe。
不幸的是,当我运行.exe文件时,没有任何反应。当我在vsc中运行代码时,代码可以正常运行。
按照在线说明进行以下步骤。
cd filelocation
移到了我的文件位置pyinstaller name-of-my-file.py
(例如,也尝试过使用--onefile
规范。)如上所述,当我运行.exe文件时没有任何反应。还尝试以管理员身份运行它。
以防万一,我将在下面发布我的代码。
感谢各种帮助。
from tkinter import *
from tkinter import messagebox
import time
import datetime
def clicked(event=None):
t = presentationDuration.get()
try:
t = float(t)
except ValueError:
messagebox.showerror(title='ValueError', message='The string is empty or there is no number entered!')
return
nSpeaker = nextSpeaker.get()
lbl.configure(text = nSpeaker, font = ("Arial Bold", 80))
t = int(t*60)
update(t)
def update(t):
if(t >= 0):
m,s = divmod(t, 60)
left_Time.configure(text = m)
right_Time.configure(text = s)
if(t <= 60):
nSpeaker = nextSpeaker.get()
lbl.configure(text = nSpeaker, bg = 'red', font = ("Arial Bold", 80))
window.after(1000, update, t-1)
window = Tk()
window.title("presenters Toolkit")
lbl_duration = Label(window, text = "duration [mins]")
lbl_duration.grid(column = 0, row = 0)
presentationDuration = Entry(window, width = 10)
presentationDuration.grid(column = 1, row = 0)
lbl_speaker = Label(window, text = "next Speaker")
lbl_speaker.grid(column = 2, row = 0)
nextSpeaker = Entry(window, width = 30)
nextSpeaker.grid(column = 3, row = 0)
lbl = Label(window, text = "", font = ("Arial Bold", 50))
lbl.grid(column = 1, row = 1)
btn = Button(window, text = "start", command = clicked)
btn.grid(column = 1, row = 3)
left_Time = Label(window, text ="--", font = ("Arial Bold", 80))
left_Time.grid(column = 0, row = 4)
mid_Time = Label(window, text = ":", font = ("Arial Bold", 80))
mid_Time.grid(column = 1, row = 4)
right_Time = Label(window, text = "--", font = ("Arial Bold", 80))
right_Time.grid(column = 2, row = 4)
window.mainloop()
答案 0 :(得分:0)
您需要将函数的代码移至上方,以便例如在导入后使用它们的代码。另外,在行t = float(presentationDuration.get ())
中运行代码时,我发现一个错误,如果在t = float(presentationDuration.get())
为空时,单击按钮ValueError: could not convert string to float
会引发异常。因此,我建议您处理这种情况。下面是我将看到的clicked
函数的代码。对不起,我的英语不是我的母语。
def clicked(event=None):
t = presentationDuration.get()
try:
t = float(t)
except ValueError:
messagebox.showerror(title='ValueError', message='The string is empty or there is no number entered!') # also you should have such line in your code `from tkinter import messagebox`
return
nSpeaker = nextSpeaker.get()
lbl.configure(text = nSpeaker, font = ("Arial Bold", 80))
t = int(t*60)
update(t)