简体   繁体   English

Kivy相当于tkinter的messagebox

[英]Kivy equivalent to tkinter's messagebox

I'm attempting to transition a program from tkinter to Kivy. 我正在尝试将程序从tkinter转换为Kivy。 When I got to a section where I used messagebox.askyesno , I figured that I could just create a popup with a couple of buttons, and I'd be done. 当我到达使用messagebox.askyesno的部分时,我想我可以创建一个带有几个按钮的弹出窗口,我就完成了。 The issue I've encountered is that, while Kivy's popup and tkinter's messagebox are both modal, when I call messagebox.askyesno in a function, messagebox will halt all execution of the current function until the messagebox is destroyed, while the popup will allow the function to finish. 我遇到的问题是,虽然Kivy的弹出窗口和tkinter的消息框都是模态的,但当我在函数中调用messagebox.askyesno时,messagebox将暂停当前函数的所有执行,直到消息框被销毁,而弹出窗口将允许功能完成。 My original program had 我原来的节目了

flag = messagebox.askyesno(message='...',parent=self)
if flag:
    #Stuff if flag is true
else:
    #Stuff if flag is false

However, this will not work with a Kivy popup since the popup will open, and the program will continue to execute. 但是,这将不适用于Kivy弹出窗口,因为弹出窗口将打开,程序将继续执行。 Is there a way to halt execution until the popup has been destroyed, or another way to solve the problem? 有没有办法停止执行,直到弹出窗口被破坏,或者解决问题的另一种方法?

The basic idea is a quasi-dialog for a two player game. 基本思想是双人游戏的准对话。 Here, the program asks one player if he wants to perform an action, such as move a piece. 这里,程序询问一个玩家是否想要执行动作,例如移动棋子。 If the player says "yes," then the second player is given a messagebox.askyesno for a counter-move. 如果玩家说“是”,那么第二个玩家将获得一个messagebox.askyesno用于反向移动。 A simple analogy is advancing a runner from first base to third base on a single in baseball. 一个简单的比喻就是将一名跑垒员从一垒打入三垒打入三垒。 You would have to ask the offensive team if he wants to advance the runner, or have the runner remain at second. 如果他想要推进跑步者,或者让跑步者保持在第二位,你将不得不问进攻队。 If the answer is yes, then the program would have to ask the defensive team if he wants to throw to third. 如果答案是肯定的,那么该计划将不得不向防守队员询问他是否想要投三分。 It would definitely be possible to create a function to handle each instance of askyesno, with appropriate bindings, but it seems excessive. 绝对有可能创建一个函数来处理askyesno的每个实例,并使用适当的绑定,但它似乎过多。

I'm not very familiar with how tkinter does things, but kivy requires a slightly different mental model here. 我不太熟悉tkinter如何处理事情,但kivy需要一个稍微不同的心理模型。 You don't want to stop and start the eventloop in between bits of python code, but instead probably want to start the popup, pass any state you need into it or store it somewhere else, then bind the result of the popup (eg when the user presses a 'done' button) to some new function that does the rest of your calculation. 你不想在python代码之间停止和启动eventloop,而是可能想要启动弹出窗口,将所需的任何状态传递给它或将其存储在其他地方,然后绑定弹出窗口的结果(例如,用户按下“完成”按钮)进行一些新功能,完成剩余的计算。

I can provide an example if you like, especially if you give more information about what you're trying to do. 如果你愿意,我可以提供一个例子,特别是如果你提供更多关于你想要做什么的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM