0

我想创建一个简单的 tkinter GUI,它会弹出一条带有“是”或“否”按钮的消息,但也有一个对话框。

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中完成这个?

4

1 回答 1

0

下面是一个使用以下方法构建自定义对话框的示例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", width=10, command=self.cancel)\
            .pack(side=tk.LEFT, padx=5, 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()

和输出:

在此处输入图像描述

于 2021-05-14T09:39:04.200 回答