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
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 343 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 786 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,380 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,831 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,865 Apr-16-2022, 04:04 PM
Last Post: DBox
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,546 Feb-17-2022, 07:02 AM
Last Post: pymn
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 2,795 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  "tkinter.TclError: NULL main window" Rama02 1 5,782 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  function in new window (tkinter) Dale22 7 4,964 Nov-24-2020, 11:28 PM
Last Post: Dale22
  Scrollable big image in a window (TKinter) Prospekteur 3 4,409 Sep-14-2020, 03:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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