Python Forum
Run more than one window together with tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run more than one window together with tkinter
#1
I'm doing a program that has loads of windows that do different functions. But I don't know how to run more than run window together. When I'm running one and try to move for another, appears that "Windows stop responding" and it just end when I close the window that I'm in. How can I fix it? I'll post two example of code, one is to register and one is to update.

Register
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import sqlite3
from subprocess import call


conn = sqlite3.connect("C:\TkinterVenda\Database\Cows.db")
c = conn.cursor()


class Secagem:

    def __init__(self, master):
        self.master = master
        self.heading = Label(master, text='Secagem', font='arial 40 bold', bg='medium slate blue')
        self.heading.place(x=300, y=0)

        self.Nome_L = Label(master, text='Nome:', font='arial 18 bold', bg='medium slate blue')
        self.Nome_L.place(x=0, y=70)

        self.Data_L = Label(master, text='Data da Secagem:', font='arial 18 bold', bg='medium slate blue')
        self.Data_L.place(x=0, y=120)

        self.Anls_L = Label(master, text='AnĂ¡lise OnFarm:', font='arial 18 bold', bg='medium slate blue')
        self.Anls_L.place(x=0, y=170)

        self.Produtos_L = Label(master, text='Produtos', font='arial 18 bold', bg='medium slate blue')
        self.Produtos_L.place(x=0, y=220)

        nomes = []
        comando = "Select * FROM Cadastro"
        c.execute(comando)
        dados = c.fetchall()
        linhas = len(dados)

        for i in range(2, linhas):
            for j in range(2, 3):
                nomes.append(dados[i][j])
        self.Nome_E = ttk.Combobox(master, width=20, font='arial 14 bold')
        self.Nome_E['values'] = nomes
        self.Nome_E.grid(column=1, row=0)
        self.Nome_E.place(x=250, y=80)

        ano = []
        valores = []
        for z in range(1, 32):
            valores.append(z)
        for d in range(2010, 2026):
            ano.append(d)

        self.comb2 = ttk.Combobox(master, width=10, state='readonly')
        self.comb2['values'] = valores
        self.comb2.grid(column=1, row=0)
        self.comb2.place(x=250, y=130)

        self.comb = ttk.Combobox(master, width=10, state='readonly')
        self.comb['values'] = ['01', '02', '03', '04', '05', '06', '07', '08', '09',
                               '10', '11', '12']
        self.comb.grid(column=1, row=0)
        self.comb.place(x=380, y=130)

        self.comb3 = ttk.Combobox(master, width=10, state='readonly')
        self.comb3['values'] = ano
        self.comb3.grid(column=1, row=0)
        self.comb3.place(x=505, y=130)

        self.Anls_E = Entry(master, width=25, font='arial 18 bold')
        self.Anls_E.place(x=250, y=170)

        self.Produtos_E = Entry(master, width=25, font='arial 18 bold')
        self.Produtos_E.place(x=250, y=220)

        self.BotaoSalvar = Button(master, width=20, height=2, bg='DarkGoldenrod1', text='Salvar',
                                  activebackground='DarkGoldenrod1', cursor='hand2', command=self.save)
        self.BotaoSalvar.place(x=380, y=320)

        self.BotaoSalvar = Button (master, width=20, height=2, bg='PaleTurquoise1', text='Alterar',
                                   activebackground='PaleTurquoise1', cursor='hand2', command=self.secagemUPD)
        self.BotaoSalvar.place (x=600, y=320)

        self.master.bind("<Return>", self.save)

    def save(self,*args, **kwargs):
        dia = self.comb2.get ()
        mes = self.comb.get ()
        ano = self.comb3.get ()
        dat = ano + '-' + mes + '-' + dia

        self.Nome = self.Nome_E.get()
        self.Data = dat
        self.Analise = self.Anls_E.get()
        self.Dia = dia
        self.Mes = mes
        self.Ano = ano
        self.Produtos = self.Produtos_E.get()

        sql = 'INSERT INTO Secagem (Nome, Data, Analise, Produto, Dia, Mes, Ano) VALUES(?,?,?,?,?,?,?)'
        c.execute(sql,(self.Nome, self.Data, self.Analise, self.Produtos, self.Dia, self.Mes, self.Ano))
        conn.commit()

        tkinter.messagebox.showinfo("Provedor", "Dados Salvos Com Sucesso!")

        self.Nome_E.set('')
        self.comb.set('')
        self.comb2.set('')
        self.comb3.set('')
        self.Anls_E.delete(0, END)
        self.Produtos_E.delete(0, END)

    def search(self):
        search = "SELECT * FROM Secagem WHERE Nome = ?"
        result = c.execute (search, (self.Nome_E.get (),))

        for r in result:
            self.n1 = r[1]
            self.n2 = r[2]
            self.n3 = r[3]
            self.n4 = r[4]
            self.n5 = r[5]
            self.n6 = r[6]
            self.n7 = r[7]

        self.clear()
        self.Nome_E.set(self.n1)
        self.comb2.set(self.n5)
        self.comb.set(self.n6)
        self.comb3.set(self.n7)
        self.Anls_E.insert(0, str(self.n3))
        self.Produtos_E.insert(0, str(self.n4))


    def clear(self):
        self.Nome_E.set('')
        self.comb2.set('')
        self.comb.set('')
        self.comb3.set('')
        self.Anls_E.delete(0, END)
        self.Produtos_E.delete(0, END)

    def secagemUPD(self):
        call(['python', 'SecagemUpd.py'])


root = Tk()
g = Secagem(root)
root.configure(bg='medium slate blue')
root.geometry('800x400+400+10')
root.mainloop()
Update:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import sqlite3

conn = sqlite3.connect("C:\TkinterVenda\Database\Cows.db")
c = conn.cursor()


class Secagem:

    def __init__(self, master):
        self.master = master
        self.heading = Label(master, text='Secagem', font='arial 40 bold', bg='medium slate blue')
        self.heading.place(x=300, y=0)

        self.Nome_L = Label(master, text='Nome:', font='arial 18 bold', bg='medium slate blue')
        self.Nome_L.place(x=0, y=70)

        self.Data_L = Label(master, text='Data da Secagem:', font='arial 18 bold', bg='medium slate blue')
        self.Data_L.place(x=0, y=170)

        self.Anls_L = Label(master, text='AnĂ¡lise OnFarm:', font='arial 18 bold', bg='medium slate blue')
        self.Anls_L.place(x=0, y=220)

        self.Produtos_L = Label(master, text='Produtos', font='arial 18 bold', bg='medium slate blue')
        self.Produtos_L.place(x=0, y=270)

        nomes = []
        comando = "Select * FROM Secagem"
        c.execute(comando)
        dados = c.fetchall()
        linhas = len(dados)

        for i in range(2, linhas):
            for j in range(1, 2):
                nomes.append(dados[i][j])
        self.Nome_E = ttk.Combobox(master, width=20, font='arial 14 bold')
        self.Nome_E['values'] = nomes
        self.Nome_E.grid(column=1, row=0)
        self.Nome_E.place(x=250, y=80)

        self.BotaoPesquisar = Button (master, width=25, height=2, bg='Orange', text='Pesquisar',
                                      activebackground='Orange', cursor='hand2', command=self.search)
        self.BotaoPesquisar.place (x=400, y=120)

        ano = []
        valores = []
        for z in range(1, 32):
            valores.append(z)
        for d in range(2010, 2026):
            ano.append(d)

        self.comb2 = ttk.Combobox(master, width=10, state='readonly')
        self.comb2['values'] = valores
        self.comb2.grid(column=1, row=0)
        self.comb2.place(x=250, y=180)

        self.comb = ttk.Combobox(master, width=10, state='readonly')
        self.comb['values'] = ['01', '02', '03', '04', '05', '06', '07', '08', '09',
                               '10', '11', '12']
        self.comb.grid(column=1, row=0)
        self.comb.place(x=380, y=180)

        self.comb3 = ttk.Combobox(master, width=10, state='readonly')
        self.comb3['values'] = ano
        self.comb3.grid(column=1, row=0)
        self.comb3.place(x=505, y=180)

        self.Anls_E = Entry(master, width=25, font='arial 18 bold')
        self.Anls_E.place(x=250, y=220)

        self.Produtos_E = Entry(master, width=25, font='arial 18 bold')
        self.Produtos_E.place(x=250, y=270)

        self.BotaoAtualizar = Button (master, width=20, height=2, cursor='hand2',
                                      text='Atualizar', bg='grey', activebackground='grey', command=self.update)
        self.BotaoAtualizar.place (x=550, y=320)

        self.master.bind("<Return>", self.update)


    def search(self):
        search = "SELECT * FROM Secagem WHERE Nome = ?"
        result = c.execute (search, (self.Nome_E.get (),))

        for r in result:
            self.n1 = r[1]
            self.n2 = r[2]
            self.n3 = r[3]
            self.n4 = r[4]
            self.n5 = r[5]
            self.n6 = r[6]
            self.n7 = r[7]
            self.n8 = r[8]

        self.clear()
        self.Nome_E.set(self.n1)
        self.comb2.set(self.n5)
        self.comb.set(self.n6)
        self.comb3.set(self.n7)
        self.Anls_E.insert(0, str(self.n3))
        self.Produtos_E.insert(0, str(self.n4))

    def clear(self):
        self.Nome_E.set('')
        self.comb2.set('')
        self.comb.set('')
        self.comb3.set('')
        self.Anls_E.delete(0, END)
        self.Produtos_E.delete(0, END)

    def update(self):
        dia = self.comb2.get ()
        mes = self.comb.get ()
        ano = self.comb3.get ()
        dat = ano + '-' + mes + '-' + dia

        self.u1 = self.Nome_E.get ()
        self.u2 = dat
        self.u3 = self.Anls_E.get()
        self.u4 = self.Produtos_E.get()
        self.u5 = dia
        self.u6 = mes
        self.u7 = ano

        UPD = "UPDATE Secagem SET Nome = ?, Data = ?, Analise = ?, Produto = ?, Dia = ?, Mes = ?, Ano = ? WHERE " \
              "ID = ?"
        c.execute (UPD, (self.u1, self.u2, self.u3, self.u4, self.u5, self.u6, self.u7, self.n8,))
        conn.commit ()
        tkinter.messagebox.showinfo ("Provedor", "Dados Atualizados Com Sucesso!")
        self.clear ()

root = Tk()
g = Secagem(root)
root.configure(bg='medium slate blue')
root.geometry('800x400+500+50')
root.mainloop()
Reply
#2
could you supply a sample Cows database, can't run code without
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scrollable big image in a window (TKinter) Prospekteur 3 214 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Adding an image to a tkinter window djwilson0495 2 541 Aug-23-2020, 11:07 AM
Last Post: ebolisa
  [Tkinter] calling a new window from a tkinter window neuroprogrammer 2 286 Jul-28-2020, 10:59 PM
Last Post: deanhystad
  How to display results from terminal window onto tkinter. buttercup 0 200 Jul-21-2020, 04:41 AM
Last Post: buttercup
  Tkinter: multitab window WizardMitMagi 3 489 Apr-29-2020, 02:40 PM
Last Post: deanhystad
  tkinter.TclError: bad window path name kenwatts275 3 1,385 Apr-26-2020, 08:16 PM
Last Post: kenwatts275
  Using Tkinter widgets on child window chewy1418 8 1,463 Feb-27-2020, 10:34 PM
Last Post: Marbelous
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 1,309 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 944 Dec-16-2019, 04:47 AM
Last Post: woooee
  [Tkinter] Window geometry appears different on Win and Linux steve_shambles 6 1,207 Nov-29-2019, 12:30 AM
Last Post: steve_shambles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020