When I run code outside of IDLE (PYTHON 3.7) the created Tkinter window does not appear

Asked

Viewed 122 times

0

I’m beginner and was studying Tkinter and developed this code, within IDLE it works perfectly, but with advancing I turned it into executable but does not rotate, even inside the cmd does not rotate. The Tkinter box does not appear. Someone can tell me what is wrong?

Program:

    from tkinter import *
from Usuarios import Usuarios


class Application:

    def __init__ (self, master):     
        self.fontp = ('Arial', '12')

        self.c1 = master

        Label(self.c1, text='Dados financeiros'
              , font=('Arial', '12', 'bold')).grid (column=2, row=1, pady=5)

        Label(self.c1, text='Nome: ', font=self.fontp, width=15).grid(column=1, row=2, pady=9)
        self.nome = Entry(self.c1, width=23)
        self.nome.grid(column=2, row=2)
        self.nome.focus_force() # Para o foco começar neste campo        
        self.busca = Button(self.c1, text='Buscar',font=self.fontp, width = 8)
        self.busca.grid(column=3, row=2, padx=9)
        root.bind('<Return>', self.buscanome)#enter para buscar
        self.busca.bind('<Any-Button>', self.buscanome)


        self.salario = Label(self.c1, text='Salario fixo: ', font=self.fontp, width=15)
        self.salario.grid(column=1, row=3, pady=5)
        self.salario1 = Entry(self.c1, width=23)
        self.salario1.grid(column=2, row=3)


        self.gastof = Label(self.c1, text='Gasto fixo Mensal: ', font=self.fontp, width=15)
        self.gastof.grid(column=1, row=4, pady=5)
        self.gastof1 = Entry(self.c1, width=23)
        self.gastof1.grid(column=2, row=4)


        self.gasto = Label(self.c1, text='Gasto: ', font=self.fontp, width=15)
        self.gasto.grid(column=1, row=5, pady=9)
        self.gasto1 = Entry(self.c1, width=23, state ='disabled')
        self.gasto1.grid(column=2, row=5)
        self.att = Button(self.c1, text='Adicionar',font=self.fontp, width = 8)
        self.att.grid(column=3, row= 5, padx=9)
        self.att['command'] = self.attgasto


        self.gastot = Label(self.c1, text='Gasto Total: ', font=self.fontp, width=15)
        self.gastot.grid(column=1, row=6, pady=5)
        self.gastot1 = Entry(self.c1, width=23, state ='disabled')
        self.gastot1.grid(column=2, row=6)


        self.gastop = Label(self.c1, text='Gasto previsto \nmensal: ', font=self.fontp, width=15)
        self.gastop.grid(column=1, row=7, pady=5)
        self.gastop1 = Entry(self.c1, width=23)
        self.gastop1.grid(column=2, row=7)


        self.ormt = Label(self.c1, text='', font=('Arial', '10'), width=20)
        self.ormt.grid(column=2, row=8, pady=5)


        self.cadastro = Button(self.c1, text='Cadastrar \nnovo usuário',font=self.fontp, width=12)
        self.cadastro['command'] = self.cadastrar
        self.cadastro.grid(column=1, row=9, columnspan=2, pady=5)

        self.attm = Button(self.c1, text='Atualização \nmensal',font=self.fontp, width=12)
        self.attm['command'] = self.atualizarmensal
        self.attm.grid(column=2, row=9, columnspan=2)



    def buscanome (self,event):
        user = Usuarios()

        nomeb = self.nome.get()

        self.ormt['text']= user.selectUser(nomeb)

        self.nome.delete(0,END)
        self.nome.insert(INSERT,user.nome)
        ########################
        verif = self.nome.get()
        if verif != '':
            self.gasto1.configure(state ='normal')
            self.gastot1.configure(state ='normal')
        else:
            self.gastot1.delete(0,END)
            self.gasto1.delete(0,END)
            self.gasto1.configure(state ='disabled')
            self.gastot1.configure(state ='disabled')
        ###########################################
        self.salario1.delete(0,END)
        self.salario1.insert(INSERT,user.salariofixo)
        self.gastof1.delete(0,END)
        self.gastof1.insert(INSERT,user.gastofixo)
        self.gastot1.delete(0,END)
        self.gastot1.insert(INSERT,user.gastototal)
        self.gastop1.delete(0,END)
        self.gastop1.insert(INSERT,user.gastoprevisto)





    def attgasto (self):
        user=Usuarios()

        nomeb = self.nome.get()

        a=0
        g1 = float(self.gastot1.get())
        go1 = float(self.gasto1.get())
        a =  g1+go1

        gastototala = a

        self.ormt['text']= user.updateUser(gastototala,nomeb)                     

        self.gastot1.delete(0,END)
        self.gasto1.delete(0,END)
        #busca de novo para retornar o valor
        self.ormt['text']= user.selectUser(nomeb)
        self.gastot1.insert(INSERT,user.gastototal)



    def cadastrar (self):
        user=Usuarios()


        user.nome = self.nome.get()
        user.salariofixo = self.salario1.get()
        user.gastofixo = self.gastof1.get()
        user.gastototal = self.gastof1.get()
        user.gastoprevisto = self.gastop1.get()



        self.ormt['text']= user.insertUser()

        self.nome.delete(0,END)
        self.salario1.delete(0,END)
        self.gastof1.delete(0,END)
        self.gasto1.delete(0,END)
        self.gastot1.delete(0,END)
        self.gastop1.delete(0,END)


    def atualizarmensal (self):
        user=Usuarios()

        nomeb = self.nome.get()

        user.nome = self.nome.get()
        user.salariofixo = self.salario1.get()
        user.gastofixo = self.gastof1.get()
        user.gastototal = self.gastof1.get()
        user.gastoprevisto = self.gastop1.get()

        self.ormt['text']= user.updateattUser(nomeb)

        self.nome.delete(0,END)
        self.nome.insert(INSERT,user.nome)
        self.salario1.delete(0,END)
        self.salario1.insert(INSERT,user.salariofixo)
        self.gastof1.delete(0,END)
        self.gastof1.insert(INSERT,user.gastofixo)
        self.gastot1.delete(0,END)
        self.gastot1.insert(INSERT,user.gastototal)
        self.gastop1.delete(0,END)
        self.gastop1.insert(INSERT,user.gastoprevisto)




root = Tk()
Application(root)
root.resizable(width=False, height=False)
root.title('Controle de Finanças')
root.geometry('400x400')
#root.configure(bg='black')
root.mainloop

It calls two other Codes "Users" "Database"

Users:

from Banco import Banco

class Users(Object):

def __init__(self, nomeb="", nome = "", salariofixo = "", gastofixo = "",
             gastototal = "", gastoprevisto = "", gastototala=''):
    self.info = {}
    self.nomeb = nomeb
    self.nome = nome
    self.salariofixo = salariofixo
    self.gastofixo = gastofixo
    self.gastototal = gastototal
    self.gastoprevisto = gastoprevisto
    self.gastototala = gastototala


def insertUser(self):

    banco = Banco()
    try:

        c = banco.conexao.cursor()

        c.execute("insert into usuarios (nome, salariofixo, gastofixo, gastototal, gastoprevisto) values ('"
                    + self.nome + "', '" + self.salariofixo + "', '" + self.gastofixo + "', '" + self.gastototal +
                    "', '" + self.gastoprevisto + "' )")

        banco.conexao.commit()
        c.close()
        return "Conta cadastrado \ncom sucesso!"
    except:
        return "Ocorreu um erro \nna inserção da conta"

def updateUser(self,gastototala,nomeb):

    banco = Banco()
    try:

        c = banco.conexao.cursor()

        c.execute('''UPDATE usuarios SET gastototal = ? WHERE nome = ?''',(gastototala, nomeb))


        banco.conexao.commit()
        c.close()

        return "Conta atualizada \ncom sucesso!"
    except:
        return "Ocorreu um erro \nna alteração da conta"


def updateattUser(self,nomeb):

    banco = Banco()
    try:

        c = banco.conexao.cursor()

        c.execute('''UPDATE usuarios SET salariofixo = ?, gastofixo = ?, gastototal = ?, gastoprevisto = ?
                    WHERE nome = ?''',(self.salariofixo, self.gastofixo, self.gastototal, self.gastoprevisto, nomeb))


        banco.conexao.commit()
        c.close()

        return "Conta atualizada \ncom sucesso!"
    except:
        return "Ocorreu um erro \nna alteração da conta"


def selectUser(self, nomeb):
    banco = Banco()
    try:

        c = banco.conexao.cursor()


        c.execute('select * from usuarios where nome = ?', (nomeb,))
        #c.execute("""select * from usuarios;""") #IMPRIME TODOS OS 
        #for lin in c.fetchall():
            #print(lin)

        for linha in c:#.fetchall():                   
            #print(linha)

            self.nome = linha[0]
            self.salariofixo = linha[1]
            self.gastofixo = linha[2]
            self.gastototal = linha[3]
            self.gastoprevisto = linha[4]

        if self.nome == '':
            return 'Nenhum nome encontrado'

        c.close()

        return "Busca feita \ncom sucesso!"
    except:

        return "Ocorreu um erro \nna busca da conta"

Bench:

import sqlite3

class Banco():

def __init__(self):
    self.conexao = sqlite3.connect('bancofinancas.db')
    self.createTable()

def createTable(self):
    c = self.conexao.cursor()

    c.execute("""create table if not exists usuarios (
                 nome text,
                 salariofixo float,
                 gastofixo float,
                 gastototal float,
                 gastoprevisto float)""")
    self.conexao.commit()
    c.close()

1 answer

0

For those who had this problem, I found a silly mistake, I found that Root.mainloop needs '()' to work, getting root.mainloop(), if they find another error in the code, comment.

Browser other questions tagged

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