how to prevent messsagebox from opening another window?

0

I have difficulties with tkinter and it turns out that I have created a root window, which inherits UI.

from tkinter import *
from ttkthemes import  themed_tk as tk
from tkinter import ttk
import tkinter.messagebox as tmb

class UI(Frame):
      def __init__(self,parent=None):
           Frame.__init__(self,parent)
           self.parent = parent
           self.parent = tk.ThemedTk()
           self.parent.set_theme("arc")
           self.parent.geometry("480x520")
           self.init_ui()
      def init_ui(self):
          self.parent.title("Nueva ventana heredada")
          ventana = ttk.Frame(self.parent)
          ventana.pack()
          entrada = ttk.Entry(ventana,text="ingresa")
          entrada.grid(row=2,column=2)
          #---------------------------------------------------
          def ver():
              try:
                res = int(entrada.get())
                print(res)
              except ValueError:
                    tmb.showwarning(title="error",message=" error")
          #-------------------------------------------------    
         boton = ttk.Button(ventana,text="pulsame", command=ver)
         boton.grid(row=2, column=3)        
if __name__== '__main__':
     root = Tk()
     sug = Label(root, text="aqui es para escribir")
     sug.pack()
     app = UI(parent=root)
     app.mainloop()
     root.destroy()

As you can see, when I press the button, a number should appear on the console, of course if you wrote it, otherwise two windows will appear and one of those is

tmb.showwarning(title="error",message=" error")

the other one is the root window, I want there to be no other window and I suspect the topic that I have given it.

    self.parent = tk.ThemedTk()
    self.parent.set_theme("arc")

Could you help me please?

    
asked by royer 19.11.2017 в 01:17
source

1 answer

0

The problem is that you are effectively overwriting the parent of your window in the initializer of the UI class.

To use ThemedTk, which is actually a wrapper over tkinter.TK , you should do it by creating root , not in the UI class, ie do:

if __name__ == "__main__":
    root = tk.ThemedTk()
    root.set_theme("arc")
    sug = tk.Label(root, text="aqui es para escribir")
    sug.pack()
    app = UI(parent=root)
    app.mainloop()

In any case personally I prefer to use the class ThemedStyle instead of ThemedTk . Always remember that the theme will only be applied on ttk widgets, not on tkinter natives,

Your code with some changes could be:

import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as tmb
from ttkthemes import ThemedStyle

class UI(ttk.Frame):
    def __init__(self, parent=None):
        super(UI,  self).__init__(parent)
        self.parent = parent
        style = ThemedStyle(parent)
        style.set_theme("arc")
        self.parent.geometry("480x520")
        self.init_ui()

    def init_ui(self):
        self.parent.title("Nueva ventana heredada")
        self.entrada = ttk.Entry(self, text="ingresa")
        self.entrada.grid(row=2, column=2) 
        boton = ttk.Button(self, text="pulsame", command=self.ver)
        boton.grid(row=2, column=3)
        self.pack()

    def ver(self):
        try:
            res = int(self.entrada.get())
            print(res)
        except ValueError:
            tmb.showwarning(title = "error", message = " error")

if __name__== '__main__':
    root = tk.Tk() 
    sug = tk.Label(root, text="aqui es para escribir")
    sug.pack()
    app = UI(parent=root)
    app.mainloop()

Two recommendations on good practices and compliance with PEP-8, it is important not to use except the most justified cases the import model from módulo import * , on the other hand it is advisable to always use 4 spaces per level of indentation and avoid tabulations.

    
answered by 19.11.2017 в 03:38