0

I am using Tkinter to create my UI on python.

Currently, the .__init__() and .initialize() are like this:

def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
    def initialize(self):
        self.grid()
        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)#Entry é um nome de Textfield da tela
        self.entry.grid(column=0,row=0,sticky='EW')#EW é pra ele grudar nas edges
        self.entry.bind("<Return>", self.OnPressEnter)#dispara onPressEnter quando enter é pressionado no ttext field        
        self.entryVariable.set(u"Entre com o nome do amigo que você quer convidar pra sair")        

        button = Tkinter.Button(self,text=u"Ver compatibilidade!",
                                command=self.OnButtonClick)#botao clicavel dispara onButtonClick
        button.grid(column=1,row=0)
        self.labelVariable = Tkinter.StringVar()
        #label = Tkinter.Label(self,textvariable=self.labelVariable, # label que usa variável labelVariable como texto
                              #anchor="w",fg="white",bg="black", height=35, width=55)#NOVO WIDTH E HEIGHT FIXO
        #PESQUISAR COMO SE ADD SCROLLBAR PRA LABEL, SE TEM COMO OU ADD LABEL EM WINDOW E AIH BOTAR SCROLLBAR
        self.texto = Tkinter.Text(self, fg="white",bg="black", height=35, width=55)
        self.texto.grid(column=0,row=1,columnspan=2,sticky='EW')
        # create a Scrollbar and associate it with txt
        scrollb = Tkinter.Scrollbar(self, command=self.texto.yview)
        scrollb.grid(row=1, column=1, sticky='nsew')
        self.texto['yscrollcommand'] = scrollb.set        

        #label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set(u"Hello !")
        self.grid_columnconfigure(0,weight=1)#estica a coluna 1 mesmo com resize da janela
        self.resizable(True,True)#soh ppode resize horizontalmente! vertical nao pode
        self.update()
        self.geometry(self.geometry())          
        self.entry.focus_set()#textfield foca
        self.entry.selection_range(0, Tkinter.END)

On this GUI, I have a button, and on this button, I do this:

def OnButtonClick(self):
        #self.labelVariable.set( self.labelVariable.get() + "\n" + self.entryVariable.get()+" (You clicked the button)" ) #muda o texto da labelVariable com o valor de entryVariable
        #self.texto.insert(Tkinter.END, self.entryVariable.get() + "\n")
        from AcharCompatibilidadeEntreAmigosTudoJunto import AcharCompatibilidadeEntreAmigosTudoJunto
        achaCompatibilidade = AcharCompatibilidadeEntreAmigosTudoJunto(self.access_token, self)        
        achaCompatibilidade.calcularCompatibilidadeEntreEsseAmigoETodosOsMeusAmigos(self.entryVariable.get())        
        self.entry.focus_set()#seleciona o texto todo assim que o usuário aperta botão ou enter
        self.entry.selection_range(0, Tkinter.END) 

The problem is that I want to change the cursor of my mouse to a loading cursor when the user clicks on the button and it will have to change back when the .onButtonClick() function ends. How do i do that?

1 Answer 1

1

Have the button pressed call a method which sets the cursor into whatever you want it to, and then in the same method start a new thread of the actual method you were trying to call. Then at the end of this method change the cursor back.

This link: http://effbot.org/zone/tkinter-busy.htm should help you with changing of the cursor, and a quick google search or search on this website should give you all the information you need for threading. The module you are looking for is called 'threading' by the way.

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried to follow the tutorial you mentioned(effbot.org/zone/tkinter-busy.htm) but i don´t have a "root = Tk()", i use "Tkinter.Tk.__init__(self,parent)". I have tried to use self instead of root and it didn´t work. Then, i have used self.root and the console keeps saying that self has no attribute config
Well, calling root.config is calling config on the object created when you would normally use root = tkinter.Tk(). Is there any reason you have chosen to create your program the way you have instead of creating an instance of Tk?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.