0

What's the easiest way (if any) of adding "Remember my answer" Checkbutton to askokcancel (tkinter messagebox) dialog? I see nothing short of making my own dialog (Toplevel window with the layout similar to askokcancel). I don't think it will be easy to conform the dialog's style. I may popup other dialog (instead of breaking the loop) to ask user if he wants that his answer was applies to all successive "error", but it looks to me no better.

if lg.name in db:
    if askokcancel(message=f'"{args.db}" already has "{lg.name}", continue?'):
        pass
    else:
        break

Using Python 3.9.2, Debian GNU/Linux 11 (bullseye)

UPDATE 2023-02-20

Not quite a solution, a workaround

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import textwrap
import tkinter as tk
from tkinter.simpledialog import Dialog


class AskOkCancel(Dialog):
    def __init__(self, master, message=None, parent=None, title="askokcancel"):
        self.message = message
        self._parent = parent
        self._title = title
        super().__init__(master)

    def body(self, master):
        self.title(self._title)
        master.rowconfigure(0, weight=1)
        tk.Label(master, text=textwrap.fill(self.message, width=40), height=3,
                 relief=tk.SUNKEN).grid(column=0, columnspan=2, row=0)
        self.chkvar = tk.IntVar()
        self.chk = tk.Checkbutton(master, variable=self.chkvar,
                                  text="Remember my answer")
        self.chk.grid(column=0, row=1, columnspan=2, sticky=tk.EW)
        self.lift(aboveThis=self._parent)
        return self.chk              # initial focus

    def ok(self, event=None):
        super().ok(event)
        self.result = True, self.chkvar.get()

    def cancel(self, event=None):
        super().cancel(event)
        self.result = False, self.chkvar.get()

I use it as below:

if lg.name in db and not (lg.name in confirmed):
    _d = AskOkCancel(
        lv, parent=lv, title='Duplicated record',
        message=(f'"{args.db}" already has "{lg.name}", '
                 f'continue?'))
    if _d.result[0]:
        if _d.result[1]:
            confirmed.add(lg.name)
    else:
        break

Where confirmed is a set()

1 Answers1

1

I think you would like reading this:

https://stackoverflow.com/questions/69559208/tkinter-messagedialog-askyesno-with-a-checkbox```
Port Me
  • 83
  • 6
  • Started from the page you mentioned. The recipes at https://stackoverflow.com/questions/10057672/correct-way-to-implement-a-custom-popup-tkinter-dialog-box to me look more promising – Vladimir Zolotykh Feb 17 '23 at 21:12