Home > database >  How can I use showdialog in Tkinter? in python
How can I use showdialog in Tkinter? in python

Time:10-16

import tkinter.commondialog
from tkinter import filedialog, Tk, Frame, Label, PhotoImage, Button, simpledialog
import form
from PIL import ImageTk, Image
import tkinter as tk
from tkinter import messagebox
import json

from tkinter.simpledialog import Dialog
from tkinter import commondialog  



class My_Class():
    def__init__(self):
        self.my_list=["a"]

     def my_function(self):

         list=["s","ss","df"]
        for i in list:
            q=tkinter.simpledialog.askstring(text="",prompt=i)

when I searched related question I did not see an answer for Python, yes django, android available but how can we implement showdialog or any method in order to resize dialogbox? related function is body() I cannot find a way to use it and resize the dialogbox.

CodePudding user response:

To change the width and height of Dialog you can do this: call .geometry in the body method:

from tkinter import Tk
from tkinter.simpledialog import Dialog


class MyDialog(Dialog):
    def __init__(self, parent, title=None, width=300, height=200):
        # all variables should be initialized before calling 
        # `super` because it calls .wait_window
        self.width = width
        self.height = height
        super().__init__(parent, title)

    def body(self, master):
        self.geometry(f'{self.width}x{self.height}')


root = Tk()
root.withdraw()

MyDialog(root)

CodePudding user response:

Well if you want to change the geometry of tkinter.simpledialog.ask[string, integer, float] then it has to be done in a way like this:

from tkinter import Tk, Button
from tkinter.simpledialog import askstring, Dialog


def change_geometry():
    widget = root.winfo_children()[-1]
    if isinstance(widget, Dialog):
        widget.geometry('500x500')


def show_dialog():
    root.after(10, change_geometry)
    return askstring('Title', 'prompt', parent=root)


root = Tk()
root.geometry('300x200')

Button(root, text='ask string', command=show_dialog).pack()

root.mainloop()

Have to schedule a call after opening the dialog to a function that will get the children of the root window and as the dialog's parent is set as root (also automatically but better be explicit) then it will also appear in that list, it should be the last one but there is a check in place to check if it actually is an instance of Dialog (since Dialog is somewhere up the inheritance chain and it inherits from Toplevel which has the .geometry method which allows to change the geometry of the dialog).

  • Related