我有一个简单的问题。我找不到解决方案。我试图在程序创建的每个窗口中放置一个图标。在主窗口中,有了它,现在我想尝试与TopLevel()使用相同的函数,但是我不能。它说TopLevel没有Call属性。请帮帮我!下面的代码。
from tkinter import ttk
from tkinter import *
import tkinter
import sqlite3
class griffo:
db_name = 'database.db'
def __init__(self, window):
self.wind = window
self.wind.title('GRIFFO HISTORYPEDIA')
#This is the code to insert a icon on the main WINDOW
icon = PhotoImage(file='griffoicone.png')
self.wind.call('wm', 'iconphoto', self.wind._w, icon)
self.wind.configure(background='gray15')
framename = LabelFrame(self.wind, text = '', foreground="azure")
framename.grid(row = 0, column = 0, columnspan = 3, pady = 5)
framename.configure(background='gray15')
frame = LabelFrame(self.wind, text = '', foreground="azure")
frame.grid(row = 1, column = 0, columnspan = 3, pady = 5)
frame.configure(background='gray15')
#I WANT TO PUT A ICON IN THIS TOPLEVEL
self.credits_wind = Toplevel()
self.credits_wind.title = 'GNU'
self.credits_wind.configure(background='gray20')
framecredits = LabelFrame(self.credits_wind, text = '', foreground="azure")
framecredits.pack(side="top", fill="both", expand=True)
framecredits.configure(background='gray15')
Label(framecredits, text = 'LICENÇA DE USO', foreground="azure", background ='gray15').grid(row = 1, column = 0)
self.credits = Text(framecredits, width=30,height=18)
self.credits.config(font=("consolas", 10), undo=True, wrap='word')
quote1 = """GRIFFO Historypedia, ferramenta pioneira para cadastro de sociedades históricas Copyright(C)2019 András Hartmann Pataki
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>5
To contact, send a email to: andras.h.pataki@gmail.com or edu.pataki@gmail.com
"""
self.credits.insert(END, quote1)
self.credits.grid(row = 0, column = 0)
self.scrollcredit = Scrollbar(framecredits, command=self.credits.yview)
self.scrollcredit.grid(row=0, column=1, sticky='ns', pady= 4)
self.credits['yscrollcommand'] = self.scrollcredit.set
frameimage = LabelFrame(self.credits_wind, text = '', foreground="azure")
frameimage.pack(side="left", fill="both", expand=True)
frameimage.configure(background='gray15')
imagem = PhotoImage(file="griffologo.png")
w = Label(frameimage, image=imagem)
w.imagem = imagem
w.pack()
#ENTRADA DO NOME------------------------------------------------------------------------------------
Label(framename, text = 'Insira o Nome da Sociedade', foreground="azure", background ='gray15').grid(row = 0, column = 0)
self.name = Entry(framename, width=20)
self.name.config(font=("consolas", 12))
self.name.focus()
self.name.grid(row = 0, column = 1)
#BOTÃO PARA PESQUISA------------------------------------------------------------------------------------
ttk.Button(framename, text = 'PESQUISAR', command = self.search_griffo).grid(row = 0, column = 2)
#ENTRADA DO ASPECTO GEOGRAFICO------------------------------------------------------------------------------------
Label(frame, text = 'Caracteristica Geografica: ', foreground="azure", background ='gray15').grid(row = 1, column = 0)
self.geografia = Text(frame, width=32,height=10)
self.geografia.config(font=("consolas", 10), undo=True, wrap='word')
self.geografia.focus()
self.geografia.grid(row = 2, column = 0, pady=4)
#BARRA DE SCROLL ASPECTO GEOGRAFICO------------------------------------------------------------------------------------
self.scrollb = Scrollbar(frame, command=self.geografia.yview)
self.scrollb.grid(row=2, column=1, sticky='ns', pady= 4)
self.geografia['yscrollcommand'] = self.scrollb.set
#ENTRADA DO PILAR Econoomico------------------------------------------------------------------------------------
Label(frame, text = 'Pilar Econômico: ', foreground="azure", background ='gray15').grid(row = 1, column = 2)
self.economico = Text(frame,width=32,height=10)
self.economico.config(font=("consolas", 10), undo=True, wrap='word')
self.economico.focus()
self.economico.grid(row = 2, column = 2, pady=4)
#BARRA DE SCROLL DO PILAR ECONOMICO------------------------------------------------------------------------------------
self.scrollb = Scrollbar(frame, command=self.economico.yview)
self.scrollb.grid(row=2, column=3, sticky='ns', pady=4)
self.economico['yscrollcommand'] = self.scrollb.set
#ENTRADA DO PILAR SOCIAL------------------------------------------------------------------------------------
Label(frame, text = 'Pilar Social: ', foreground="azure", background ='gray15').grid(row = 1, column = 4)
self.social = Text(frame,width=32,height=10)
self.social.config(font=("consolas", 10), undo=True, wrap='word')
self.social.focus()
self.social.grid(row = 2, column = 4, pady=4)
#BARRA DE SCROLL DO PILAR SOCIAL------------------------------------------------------------------------------------
self.scrollb = Scrollbar(frame, command=self.social.yview)
self.scrollb.grid(row=2, column=5, sticky='ns', pady=4)
self.social['yscrollcommand'] = self.scrollb.set
#ENTRADA DO PILAR TECNOLOGICO------------------------------------------------------------------------------------
Label(frame, text = 'Pilar Tecnológico: ', foreground="azure", background ='gray15').grid(row = 3, column = 0)
self.tecnologico = Text(frame,width=32,height=10)
self.tecnologico.config(font=("consolas", 10), undo=True, wrap='word')
self.tecnologico.focus()
self.tecnologico.grid(row = 4, column = 0, pady=4)
#BARRA DE SCROLL DO PILAR TECNOLOGICO------------------------------------------------------------------------------------
self.scrollb = Scrollbar(frame, command=self.tecnologico.yview)
self.scrollb.grid(row=4, column=1, sticky='ns', pady=4)
self.tecnologico['yscrollcommand'] = self.scrollb.set
#ENTRADA DO PILAR CULTURAL-----------------------------------------------------------------------------------
Label(frame, text = 'Pilar Cultural: ', foreground="azure", background ='gray15').grid(row = 3, column = 2)
self.cultural = Text(frame,width=32,height=10)
self.cultural.config(font=("consolas", 10), undo=True, wrap='word')
self.cultural.focus()
self.cultural.grid(row = 4, column = 2, pady=4)
#BARRA DE SCROLL DO PILAR CULTURAL------------------------------------------------------------------------------------
self.scrollb = Scrollbar(frame, command=self.cultural.yview)
self.scrollb.grid(row=4, column=3, sticky='ns', pady=4)
self.cultural['yscrollcommand'] = self.scrollb.set
#ENTRADA DO COLABORADOR-----------------------------------------------------------------------------------
Label(frame, text = 'Visão do Colaborador: ', foreground="azure", background ='gray15').grid(row = 3, column = 4)
self.colaborador = Text(frame,width=32,height=10)
self.colaborador.config(font=("consolas", 10), undo=True, wrap='word')
self.colaborador.focus()
self.colaborador.grid(row = 4, column = 4, pady=4)
#SCROLL DO COLABORADOR-----------------------------------------------------------------------------------
self.scrollb = Scrollbar(frame, command=self.colaborador.yview)
self.scrollb.grid(row=4, column=5, sticky='ns', pady=4)
self.colaborador['yscrollcommand'] = self.scrollb.set
# 3° CONTEINER PARA O TREE TTK-----------------------------------------------------------------------------------
frametree = LabelFrame(self.wind, text = '', foreground="azure")
frametree.grid(row = 5, column = 0, columnspan = 3)
frametree.configure(background='gray15')
#TREE TTK-----------------------------------------------------------------------------------
self.tree = ttk.Treeview(frametree,columns=("Name", "Geografia", "Economia","Tecnologia","Cultura","Colaborador", ), height= 5)
self.tree.heading("#0", text="Nome", anchor= CENTER)
self.tree.column("#0",minwidth=0,width=100, stretch=NO)
self.tree.heading("#1", text="Geografia", anchor= CENTER)
self.tree.column("#1",minwidth=0,width=100, stretch=NO)
self.tree.heading("#2", text="Economia", anchor= CENTER)
self.tree.column("#2",minwidth=0,width=100, stretch=NO)
self.tree.heading("#3", text="Social", anchor= CENTER)
self.tree.column("#3",minwidth=0,width=100, stretch=NO)
self.tree.heading("#4", text= "Tecnologia", anchor= CENTER)
self.tree.column("#4", minwidth=0, width=100, stretch=NO)
self.tree.heading("#5", text= "Cultura", anchor= CENTER)
self.tree.column("#5", minwidth=0, width=100, stretch=NO)
self.tree.heading("#6", text= "Colaborador", anchor= CENTER)
self.tree.column("#6", minwidth=0, width=100, stretch=NO)
self.tree.grid(row = 4, column = 0)
self.get_griffo()
#TREE TTK BARRA DE SCROLL-----------------------------------------------------------------------------------
self.scrolltree = Scrollbar(frametree, command=self.tree.yview)
self.scrolltree.grid(row=4, column=1, sticky='ns', rowspan=1)
self.tree['yscrollcommand'] = self.scrolltree.set
#MENSAGEM DE NOTIFICAÇÃO-----------------------------------------------------------------------------------
self.message = Label(text = '', fg = 'red')
self.message.grid(row = 7, column = 0, columnspan = 3, sticky = W + E)
#4° CONTEINER PARA OS BOTÕES-----------------------------------------------------------------------------------
framebtn = LabelFrame(self.wind, text = '', foreground="azure")
framebtn.grid(row = 6, column = 0, columnspan = 3)
framebtn.configure(background='gray15')
#BOTÕES-----------------------------------------------------------------------------------
ttk.Button(framebtn, text = 'INSERIR', command = self.add_griffo).grid(row = 4, column = 0, sticky = W + E)
ttk.Button(framebtn, text = 'DELETAR', command = self.delete_griffo).grid(row = 4, column = 1, sticky = W + E)
ttk.Button(framebtn, text = 'EDITAR', command = self.edit_griffo).grid(row = 4, column = 2, sticky = W + E)
ttk.Button(framebtn, text = 'AJUDA', command = self.help).grid(row = 4, column = 3, sticky = W + E)
#FUNÇÃO PARA EXECUTAR BANCO DE DADOS-------------------------------------------------------------------------------
def run_query(self, query, parameters = ()):
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS griffo (id INTEGER PRIMARY KEY , name TEXT, geografia TEXT, economico TEXT, social TEXT, tecnologico TEXT, cultural TEXT, colaborador TEXT)")
result = cursor.execute(query, parameters)
conn.commit()
return result
#FUNÇÃO PARA CADASTRAR DADOS-------------------------------------------------------------------------------
def get_griffo(self):
# cleaning Table ----------------------------------------------------------------
# mesa de limpeza------------------------------------------------------------------
records = self.tree.get_children()
for element in records:
self.tree.delete(element)
query = 'SELECT * FROM griffo ORDER BY name DESC'
db_rows = self.run_query(query)
for row in db_rows:
self.tree.insert('', 0, text = row[1], values = (row[2], row[3], row[4], row[5], row[6], row[7]))
#VALIDAR ENTRADA DO USUARIO-----------------------------------------------------
def validation(self):
return (self.name.get()),(self.geografia.get(1.0, END), (self.economico.get(1.0, END), (self.social.get(1.0, END), (self.tecnologico.get(1.0, END), (self.cultural.get(1.0, END), (self.colaborador.get(1.0, END)))))))
#FUNÇÃO PARA ADICIONAR NOVOS DADOS NA TABELA griffo-----------------------------------------------------
def add_griffo(self):
if self.validation():
query = 'INSERT INTO griffo VALUES(NULL, ?, ?, ?, ?, ?, ?, ?)'
parameters = (self.name.get(), self.geografia.get(1.0, END), self.economico.get(1.0, END), self.social.get(1.0, END), self.tecnologico.get(1.0, END), self.cultural.get(1.0, END), self.colaborador.get(1.0, END))
self.run_query(query, parameters)
self.message['text'] = 'SOCIEDADE {} CADASTRADA COM SUCESSO'.format(self.name.get())
self.name.delete(0, END)
self.geografia.delete(1.0, END)
self.economico.delete(1.0, END)
self.social.delete(1.0, END)
self.tecnologico.delete(1.0, END)
self.cultural.delete(1.0, END)
self.colaborador.delete(1.0, END)
else:
self.message['text'] = 'CAMPOS OBRIGATÓRIOS'
self.get_griffo()
#FUNÇÃO PARA DELETAR DADOS NA TABELA griffo-----------------------------------------------------
def delete_griffo(self):
self.message['text'] = ''
try:
self.tree.item(self.tree.selection())['text'][0]
except IndexError as e:
self.message['text'] = 'SELECIONE UMA SOCIEDADE'
return
self.message['text'] = ''
name = self.tree.item(self.tree.selection())['text']
query = 'DELETE FROM griffo WHERE name = ?'
self.run_query(query, (name, ))
self.message['text'] = 'SOCIEDADE {} DELETADA COM SUCESSO'.format(name)
self.get_griffo()
#FUNÇÃO PARA EDITAR DADOS NA TABELA griffo-----------------------------------------------------
def edit_griffo(self):
self.message['text'] = ''
try:
self.tree.item(self.tree.selection())['values'][0]
except IndexError as e:
self.message['text'] = 'POR FAVOR, SELECIONE UMA SOCIEDADE'
return
name = self.tree.item(self.tree.selection())['text']
geografia = self.tree.item(self.tree.selection())['values'][0]
economico = self.tree.item(self.tree.selection())['values'][1]
social = self.tree.item(self.tree.selection())['values'][2]
tecnologico = self.tree.item(self.tree.selection())['values'][3]
cultural = self.tree.item(self.tree.selection())['values'][4]
colaborador = self.tree.item(self.tree.selection())['values'][5]
#CRIAR NOVA JANELA PARA EDIÇÃO-----------------------------------------------------
self.edit_wind = Toplevel()
self.edit_wind.title = 'Edit Civilization'
self.edit_wind.configure(background='gray20')
#CONTEINER PARA O NOVO ENTRRY NAME-------------------------------------------------------------------------
framename2 = LabelFrame(self.edit_wind, text = 'Registros atuais,janela de visualização avançada.', foreground="azure")
framename2.grid(row = 0, column = 0)
framename2.configure(background='gray20')
#ENTRY NAME-------------------------------------------------------------------------
self.nomelab = Label(framename2, text = 'Name: ', foreground="azure", background ='gray20').grid(row = 0, column = 0)
name2 = Entry(framename2, width=20)
name2.config(font=("consolas", 12))
name2.insert(END, name)
name2.grid(row = 0, column = 1)
#CONTEINER PARA JANELA GERAL
frame3 = LabelFrame(self.edit_wind, text = '', foreground="azure")
frame3.grid(row = 1, column = 0)
frame3.configure(background='gray20')
#NOVOS CAMPOS PARA SEREM ATUALIZADOS "IMPORTANTE"-----------------------------------------------------
Label(frame3, text = 'Carac.Geografica: ', foreground="azure", background ='gray20').grid(row = 1, column = 0)
geografia2 = Text(frame3, width=32,height=15)
geografia2.config(font=("consolas", 10), undo=True, wrap='word')
geografia2.insert(END, geografia)
geografia2.grid(row = 2, column = 0, pady=4)
scrollgeo = Scrollbar(frame3, command=geografia2.yview)
scrollgeo.grid(row=2, column=1, sticky='ns', pady= 4)
geografia2['yscrollcommand'] = scrollgeo.set
Label(frame3, text = 'Pilar Econômico: ', foreground="azure", background ='gray20').grid(row = 1, column = 2)
economico2 = Text(frame3, width=32,height=15)
economico2.config(font=("consolas", 10), undo=True, wrap='word')
economico2.insert(END, economico)
economico2.grid(row = 2, column = 2, pady=4)
scrolleco = Scrollbar(frame3, command=economico2.yview)
scrolleco.grid(row=2, column=3, sticky='ns', pady=4)
economico2['yscrollcommand'] = scrolleco.set
Label(frame3, text = 'Pilar Social: ', foreground="azure", background ='gray20').grid(row = 1, column = 4)
social2 = Text(frame3, width=32,height=15)
social2.config(font=("consolas", 10), undo=True, wrap='word')
social2.insert(END, social)
social2.grid(row = 2, column = 4, pady=4)
scrollso = Scrollbar(frame3, command=social2.yview)
scrollso.grid(row=2, column=5, sticky='ns', pady=4)
social2['yscrollcommand'] =scrollso.set
frame4 = LabelFrame(self.edit_wind, text = '', foreground="azure")
frame4.grid(row = 3, column = 0)
frame4.configure(background='gray20')
Label(frame4, text = 'Pilar Tecnológico: ', foreground="azure", background ='gray20').grid(row = 3, column = 0)
tecnologico2 = Text(frame4,width=32,height=15)
tecnologico2.config(font=("consolas", 10), undo=True, wrap='word')
tecnologico2.insert(END, tecnologico)
tecnologico2.grid(row = 4, column = 0, pady=4)
scrolltec = Scrollbar(frame4, command=tecnologico2.yview)
scrolltec.grid(row=4, column=1, sticky='ns', pady=4)
tecnologico2['yscrollcommand'] = scrolltec.set
Label(frame4, text = 'Pilar Cultural: ', foreground="azure", background ='gray20').grid(row = 3, column = 2)
cultural2 = Text(frame4,width=32,height=15)
cultural2.config(font=("consolas", 10), undo=True, wrap='word')
cultural2.insert(END, cultural)
cultural2.grid(row = 4, column = 2, pady=4)
scrollcul = Scrollbar(frame4, command=cultural2.yview)
scrollcul.grid(row=4, column=3, sticky='ns', pady=4)
cultural2['yscrollcommand'] = scrollcul.set
Label(frame4, text = 'Visão do Colaborador: ', foreground="azure", background ='gray20').grid(row = 3, column = 4)
colaborador2 = Text(frame4,width=32,height=15)
colaborador2.config(font=("consolas", 10), undo=True, wrap='word')
colaborador2.insert(END, colaborador)
colaborador2.grid(row = 4, column = 4, pady=4)
scrollcola = Scrollbar(frame4, command=colaborador2.yview)
scrollcola.grid(row=4, column=5, sticky='ns', pady=4)
colaborador2['yscrollcommand'] = scrollcola.set
#BOTÃO PARA EFETUAR ATUALIZAÇÂO-----------------------------------------------------
ttk.Button(
framename2, text = 'Update',
command = lambda: self.update_castro(name, name2, geografia2, economico2, social2, tecnologico2, cultural2, colaborador2)
).grid(row = 0, column = 3, sticky = W + E)
#FUNÇÃO FINAL PARA ATUALIZAR DADOS-----------------------------------------------------
def update_castro(self, old_name, name2, geografia2, economico2, social2, tecnologico2, cultural2, colaborador2):
name2 = name2.get()
geografia2 = geografia2.get(1.0, END)
economico2 = economico2.get(1.0, END)
social2 = social2.get(1.0, END)
tecnologico2 = tecnologico2.get(1.0, END)
cultural2 = cultural2.get(1.0, END)
colaborador2 = colaborador2.get(1.0, END)
query = 'UPDATE griffo set name = ?, geografia = ?, economico = ?, social = ?, tecnologico = ?, cultural = ?, colaborador = ? WHERE name = ?'
self.run_query(query, (name2, geografia2, economico2, social2, tecnologico2, cultural2, colaborador2, old_name ))
self.message['text'] = 'SOCIEDADE ATUALIZADA!'
self.get_griffo()
self.edit_wind.destroy()
def help(self):
help_wind = Toplevel()
help_wind.title = 'Edit Civilization'
help_wind.configure(background='gray20')
framehelp = LabelFrame(help_wind, text = 'ACESSE: historydatabase.startupjatek.com', foreground="azure")
framehelp.pack(side="top", fill="both", expand=True)
framehelp.configure(background='gray20')
Label(framehelp, text = 'TUTORIAL RAPIDO', foreground="azure", background ='gray15').grid(row = 1, column = 0)
self.ajuda = Text(framehelp, width=30,height=25)
self.ajuda.config(font=("consolas", 10), undo=True, wrap='word')
quote2 = """GRIFFO Historypedia TUTORIAL. Para INSERIR uma nova Sociedade, preencha todos os formulários da janela principal, inclusive o NOME. Ao terminar, aperte no Botão INSERIR para concluir a operação.(uma mensagem em vermelho aparecerá confirmando). Caso pretenda VISUALIZAR ou EDITAR uma Sociedade já existente, selecione-a na tabela e aperte em EDITAR (uma nova janela surgirá, mude os campos desejados e aperte em UPDATE). Você também pode usar o formulário "Nome" na janela principal para pesquisar uma Sociedade na Tabela. Ao terminar todo o cadastro, feche o programa. Vá na pasta do mesmo, e procure pelo arquivo: database.db. Copie ele e envie para os emails listados abaixo.
To contact, send a email to: andras.h.pataki@gmail.com or edu.pataki@gmail.com
"""
self.ajuda.insert(END, quote2)
self.ajuda.grid(row = 0, column = 0)
scrollhelp = Scrollbar(framehelp, command=self.ajuda.yview)
scrollhelp.grid(row=0, column=1, sticky='ns', pady=1)
self.ajuda['yscrollcommand'] =scrollhelp.set
#FUNÇÃO PARA PESQUISAR DADOS-----------------------------------------------------
def search_griffo(self, item=''):
children = self.tree.get_children(item)
for child in children:
text = self.tree.item(child, 'text')
if text.startswith(self.name.get()):
self.tree.selection_set(child)
return True
else:
res = self.search_griffo(child)
if res:
return True
if __name__ == '__main__':
window = Tk()
application = griffo(window)
window.mainloop()
这就是整个代码。终端通知Toplevel没有呼叫属性。