微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能有一个带有是或否按钮的 tkinter 弹出消息,并且在同一个弹出消息上有一个对话框?

如何解决我怎样才能有一个带有是或否按钮的 tkinter 弹出消息,并且在同一个弹出消息上有一个对话框?

我想创建一个简单的 tkinter GUI,它会弹出带有 YES 或 NO 按钮的消息,但还有一个对话框。

import tkinter as tk
from tkinter import simpledialog

messageBox.askyesno("test","Did you enter your name?")
my_var = simpledialog.askstring(title="Test",prompt="enter sentences:")
print(myvar)

root=tk.TK()

我不想有两个弹出窗口我只想有一个对话框大到可以输入三个句子并且有一个是或否按钮。有没有办法在python3中实现这一点?

解决方法

以下是使用 simpledialog.Dialog 构建自定义对话框的示例:

import tkinter as tk
from tkinter.simpledialog import Dialog

class MyDialog(Dialog):
    # override body() to build your input form
    def body(self,master):
        tk.Label(master,text="Enter sentences:",anchor="w").pack(fill="x")
        self.text = tk.Text(master,width=40,height=10)
        self.text.pack()
        # need to return the widget to have first focus
        return self.text

    # override buttonbox() to create your action buttons
    def buttonbox(self):
        box = tk.Frame(self)
        # note that self.ok() and self.cancel() are defined inside `Dialog` class
        tk.Button(box,text="Yes",width=10,command=self.ok,default=tk.ACTIVE)\
            .pack(side=tk.LEFT,padx=5,pady=5)
        tk.Button(box,text="No",command=self.cancel)\
            .pack(side=tk.LEFT,pady=5)
        box.pack()

    # override apply() to return data you want
    def apply(self):
        self.result = self.text.get("1.0","end-1c")

root = tk.Tk()
root.withdraw()
dlg = MyDialog(root,title="Test")
print(dlg.result)
root.destroy()

和输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。