How to focus on ttk screen. Treeview, freezing the previous screens?

Asked

Viewed 369 times

0

I made an application in Python 3.6 that calls another script in python to mount a ttk screen. Treeview with database information and that submitted data can be manipulated (change and deletion).

Turns out I can’t leave this ttk screen. Treeview with the permanent focus, since when you press "alt + tab" the focus goes to the previous screen that called this script.

Could someone help me? I tried using "Transient" but I’m not succeeding.

Part of the script that calls the other python script:

def consultacadastro( posi ):
    if str(nrbancoget) != "":
        a = ConsultaCadastroGeral.Inicio()

Part of the program that displays information ttk. Treeview:

Start of the program

from Tkinter import * from Tkinter import Tk, Button, ttk, Frame, Toplevel, Label, Entry, Stringvar from Tkinter import messagebox from Manager import Manager from Pgmfuncoes import Pgmfuncoes

class Consultacadastrogeral: def Inicio(): root = Tk() root.title("Users Query - General") Customtree(root). pack(expand=1, Fill="Both")

class Customtree(Frame): def init(self, master, *args, **kwargs): Frame.init(self, master, *args, **kwargs)

    # Define tamanhos mínimos e máximos da tela
    master.minsize( width = 500, height= 200)       
    master.maxsize( width = 500, height= 200)

    self.focus_force()

    # Define botão de sair da tela
    self.top = Frame(self)
    self.edit_button = Button(self.top, text="Sair", font = "bold", fg = "blue", command=master.destroy)
    self.edit_button.pack(side="top")
    self.top.pack(fill="x")

    self.tree = ttk.Treeview(self)

    # Define tamanho da coluna
    self.tree.column("#0",minwidth=0,width=0)

    # Cria barra de rolagem      
    barra = Scrollbar(self, orient='vertical', command=self.tree.yview)

    # Adiciona barra de rolagem
    self.tree.configure(yscroll=barra.set)
    barra.pack( side = RIGHT, fill=Y )

    self.tree["columns"] = ("one", "two", "tree", "four", "five")
    self.tree.column("one", width=100, anchor="se")
    self.tree.column("two", width=100, anchor="se")
    self.tree.column("tree",width=100, anchor="se")
    self.tree.column("four",width=100, anchor="se")
    self.tree.column("five",width=100, anchor="center")
    self.tree.heading("one", text="Número Banco")
    self.tree.heading("two", text="Número Agência")
    self.tree.heading("tree", text="Conta Corrente")
    self.tree.heading("four", text="Saldo Inicial (R$)")
    self.tree.heading("five", text="Data Inicial")

"

Thanks in advance.

2 answers

0

In this case you must insert the following commands in the root of the active application/window

master.focus_force()
master.grab_set()
  • master = Variable that is the root or Toplevel of the window
  • .focus_force() => Focus on the window when opening
  • .grab_set() => Do not let you access another open window of your program while that window is open

0

Well people, as I did not get help from the most experienced in the subject, I decided to research a lot and ended up finding the solution to my problem. I removed the call from the other python script and placed its logic in a class in the main script, where the variable of the Tk class is defined: root = Tk() Window(root) root.mainloop() and used "Transient" reporting to "root".

It seems very simple the solution now, so the tip is: when you need to call a script from another python script, which will show a screen (Treeview or not), prefer to include within a class of your script "caller" the logic of this script that would be called, avoiding this call and being able to manipulate the frames with "Transient"; I do not know if I was clear, because I have a lot to walk in the Python language and in POO, but I emphasize that it is only run behind the damage and have patience to search on the internet, because there is always someone who ends up helping you in some way, even sometimes not knowing that you are.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.