Tkinter 的 simpledialog askinteger 在 30 秒后可以关闭吗?我需要使用 simpledialog。

huangapple go评论69阅读模式
英文:

Could be closed tkinter simpledialog askinteger after 30 seconds? I need to use simpledialog

问题

My case:
我需要使用simpledialog来向Python代码输入数据。我的代码如下:

def enter_plan_week():
    root = tk.Tk()
    root.withdraw()
    value = simpledialog.askinteger(title="iiiiiii", prompt="nnnnnnnnnnn")
    if value is not None:
        kw = value
        open('kw.txt', 'w').write(str(kw))
    else:
        kw = int(open('kw.txt', 'r').read())
    return kw

如果用户在30秒内未输入整数,simpledialog窗口将被关闭,并从kw.txt中获取数据。
我需要使用simpledialog。

英文:

My case:
I need input data by simpledialog to python code. My code is:

def enter_plan_week():
    root = tk.Tk()
    root.withdraw()
    value = simpledialog.askinteger(title="iiiiiii",prompt="nnnnnnnnnnn")
    if value is not None:
        kw = value
        open('kw.txt', 'w').write(str(kw))
    else:
        kw = int(open('kw.txt', 'r').read())
    return kw

If user no enter integer after 30 seconds simpledialog window should be closed and take data from kw.txt.
I need to use simpledialog.

答案1

得分: 0

你可以使用after()来安排一个函数的执行。所以,像这样做。

def enter_plan_week():
    root = tk.Tk()
    root.withdraw()
    root.after(30*1000, root.destroy)
    value = simpledialog.askinteger(title="iiiiiii", prompt="nnnnnnnnnnn")
    ...
    return kw
英文:

You can schedule a function with the after(). So, do like this.

def enter_plan_week():
    root = tk.Tk()
    root.withdraw()
    root.after(30*1000, root.destroy)
    value = simpledialog.askinteger(title="iiiiiii",prompt="nnnnnnnnnnn")
    ...
    return kw

huangapple
  • 本文由 发表于 2023年4月17日 16:26:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定