我有一个以前工作得很好的代码。 今天,我用py2exe和其他代码一起玩,但从那时起,我所有的代码都是空窗口。 我不知道这两件事是否相关,但我可以找到解释的全部内容。
有人有想法吗? 我有点失落。
感谢您的帮助
这是我的代码:
# coding=UTF-8
import socket
import pickle
from Tkinter import *
from functools import partial
#import Image, ImageTk
hote = "127.0.0.1"
port = 443
print("Tentative de connection avec le serveur")
ok = False
while(not ok):
try:
connexion_avec_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_avec_serveur.connect((hote, port))
print("Connexion etablie avec le serveur sur le port {}".format(port))
ok = True
except:
pass
class Interface(Frame):
def __init__(self, fenetre, **kwargs):
fenetre.protocol("WM_DELETE_WINDOW", self.deco)
Frame.__init__(self, fenetre, width=1024, height=900, **kwargs)
#Banner :
#img = Image.open("include/img/banner.png")
#banner = ImageTk.PhotoImage(img)
#bannerLabel = Label(image=banner)
#bannerLabel.grid(row=1, column=1)
#bannerLabel.image = banner
self.nb_clic = 0
self.bouton_quitter = Button(self, text="Quitter", command=self.deco)
self.bouton_quitter.grid(row=32, column=1)
self.torrentsName = []
self.torrentsDesc = []
self.torrentsDate = []
self.torrentsRatio = []
self.torrentsGoTo = []
self.torrentsDown = []
self.configFenetre()
self.boutonPrev = Button(self, text="precedents", state=DISABLED)
self.boutonPrev.grid(row=32, column=2)
self.boutonSuiv = Button(self, text="Suivants", state=DISABLED)
self.boutonSuiv.grid(row=32, column=3)
# Creation de nos widgets
self.bouton_ListeTorrents = Button(self, text="Liste des torrents", command= lambda: self.sendCmd("getTorrents", None))
self.bouton_ListeTorrents.grid(row=2, column=1)
self.bouton_ListeSeries = Button(self, text="Liste des Series", command= lambda: self.sendCmd("getSpeTorrents", "series"))
self.bouton_ListeSeries.grid(row=3, column=1)
self.bouton_ListeFilms = Button(self, text="Liste des Films", command= lambda: self.sendCmd("getSpeTorrents", "films"))
self.bouton_ListeFilms.grid(row=4, column=1)
self.bouton_ListeLivres = Button(self, text="Liste des Livres", command= lambda: self.sendCmd("getSpeTorrents", "livres"))
self.bouton_ListeLivres.grid(row=5, column=1)
self.bouton_ListeJeux = Button(self, text="Liste des Jeux", command= lambda: self.sendCmd("getSpeTorrents", "jeux"))
self.bouton_ListeJeux.grid(row=6, column=1)
self.bouton_ListeMusiques = Button(self, text="Liste des Musiques", command= lambda: self.sendCmd("getSpeTorrents", "musiques"))
self.bouton_ListeMusiques.grid(row=7, column=1)
self.bouton_ListeDivers = Button(self, text="Liste des torrents Divers", command= lambda: self.sendCmd("getSpeTorrents", "divers"))
self.bouton_ListeDivers.grid(row=8, column=1)
def configFenetre(self):
i = 0
self.torrentsName.append(Label(self, text="Nom du torrent"))
self.torrentsName[0].grid(row=1, column=2)
self.torrentsDesc.append(Label(self, text="Type de fichier"))
self.torrentsDesc[0].grid(row=1, column=3)
self.torrentsDate.append(Label(self, text="Date ajout"))
self.torrentsDate[0].grid(row=1, column=4)
self.torrentsRatio.append(Label(self, text="Ratio"))
self.torrentsRatio[0].grid(row=1, column=5)
self.torrentsGoTo.append(Label(self, text="Voir la page"))
self.torrentsGoTo[0].grid(row=1, column=6)
self.torrentsDown.append(Label(self, text="Telecharger"))
self.torrentsDown[0].grid(row=1, column=7)
while i< 30:
self.torrentsName.append(Label(self, text=" "))
self.torrentsName[i+1].grid(row=i+2, column=2)
self.torrentsDesc.append(Label(self, text=" "))
self.torrentsDesc[i+1].grid(row=i+2, column=3)
self.torrentsDate.append(Label(self, text=" "))
self.torrentsDate[i+1].grid(row=i+2, column=4)
self.torrentsRatio.append(Label(self, text=" "))
self.torrentsRatio[i+1].grid(row=i+2, column=5)
self.torrentsGoTo.append(Button(self, text="Voir", state=DISABLED))
self.torrentsGoTo[i+1].grid(row=i+2, column=6)
self.torrentsDown.append(Button(self, text="DL", state=DISABLED))
self.torrentsDown[i+1].grid(row=i+2, column=7)
i+=1
def sendCmd(self, cmd, args):
msg_a_envoyer = b""
if(args == None):
msg_a_envoyer = cmd
else:
msg_a_envoyer = cmd
msg_a_envoyer += " "
msg_a_envoyer += args
msg_a_envoyer = msg_a_envoyer.encode()
connexion_avec_serveur.send(msg_a_envoyer)
self.resceive()
def resceive(self):
msg_recu = b""
chaine =b""
while(not(b"<EOT>" in msg_recu)):
msg_recu = connexion_avec_serveur.recv(1024)
if(msg_recu != b"<EOT>"):
chaine += msg_recu
if(b"<EOT>" in msg_recu):
chaine.replace(b"<EOT>", b"")
dechifre = pickle.loads(chaine)
self.dealString(dechifre, 0)
def deleteLabels(self, liste):
for label in liste:
label.grid_remove()
def getRatioColor(self, nb):
if(nb == (-1)):
return self.cget('bg')
if(nb < 1):
return '#FF0000'
if(nb >= 1 and nb < 2):
return '#FF8000'
if(nb >= 2):
return '#3ADF00'
def getBackgroundColor(self, i):
if(i%2 == 0):
return '#E0F8E0'
if(i%2 == 1):
return '#CEE3F6'
if(i == -1):
return self.cget('bg')
def dealString(self, chaine, page):
nbPage = 30
i=0
j = (page*nbPage)
nbSplit = len(chaine)
#On ajoute les entetes
self.torrentsName.append(Label(self, text="Nom du torrent"))
self.torrentsName[0].grid(row=1, column=2)
self.torrentsDesc.append(Label(self, text="Type de fichier"))
self.torrentsDesc[0].grid(row=1, column=3)
self.torrentsDate.append(Label(self, text="Date ajout"))
self.torrentsDate[0].grid(row=1, column=4)
self.torrentsRatio.append(Label(self, text="Ratio"))
self.torrentsRatio[0].grid(row=1, column=5)
#On ajoutel les torrents
while(j<nbSplit and (j<((page*nbPage)+nbPage))):
self.torrentsName[i+1].config(text=chaine[j][0], background=self.getBackgroundColor(i+1))
self.torrentsName[i+1].grid(row=i+2, column=2)
self.torrentsDesc[i+1].config(text=chaine[j][1], background=self.getBackgroundColor(i+1))
self.torrentsDesc[i+1].grid(row=i+2, column=3)
self.torrentsDate[i+1].config(text=chaine[j][2], background=self.getBackgroundColor(i+1))
self.torrentsDate[i+1].grid(row=i+2, column=4)
self.torrentsRatio[i+1].config(text=round(chaine[j][3], 2), background=self.getRatioColor(chaine[j][3]))
self.torrentsRatio[i+1].grid(row=i+2, column=5)
if(chaine[j][5] == True):
fVoir = partial(self.sendCmd, "getSpeTorrents", chaine[j][6])
self.torrentsGoTo[i+1].config(text="Voir", state=NORMAL, command = fVoir)
self.torrentsGoTo[i+1].grid(row=i+2, column=6)
else:
self.torrentsGoTo[i+1].config(text="Voir", state=DISABLED)
self.torrentsGoTo[i+1].grid(row=i+2, column=6)
i+=1
j+=1
#On fini de remplir la page
bg = self.cget('bg')
while(i<30):
self.torrentsName[i+1].config(text=" ", background=bg)
self.torrentsName[i+1].grid(row=i+2, column=2)
self.torrentsDesc[i+1].config(text=" ", background=bg)
self.torrentsDesc[i+1].grid(row=i+2, column=3)
self.torrentsDate[i+1].config(text=" ", background=bg)
self.torrentsDate[i+1].grid(row=i+2, column=4)
self.torrentsRatio[i+1].config(text=" ", background=bg)
self.torrentsRatio[i+1].grid(row=i+2, column=5)
self.torrentsGoTo[i+1].config(text="Voir", state=DISABLED)
self.torrentsGoTo[i+1].grid(row=i+2, column=6)
i+=1
if(page > 0):
self.boutonPrev.config(command = lambda: self.dealString(chaine, page-1), state = NORMAL)
self.boutonPrev.grid(row=32, column=2)
else:
self.boutonPrev.config(state = DISABLED)
self.boutonPrev.grid(row=32, column=2)
if(nbSplit>(page*nbPage)+nbPage):
self.boutonSuiv.config(command = lambda: self.dealString(chaine, page+1), state = NORMAL)
self.boutonSuiv.grid(row=32, column=3)
else:
self.boutonSuiv.config(state = DISABLED)
self.boutonSuiv.grid(row=32, column=3)
def deco(self):
msg_a_envoyer = b"deconnect"
connexion_avec_serveur.send(msg_a_envoyer)
self.quit()
fenetre = Tk()
interface = Interface(fenetre)
interface.mainloop()
connexion_avec_serveur.close()
interface.destroy()
答案 0 :(得分:2)
fenetre = Tk()
interface = Interface(fenetre)
interface.mainloop()
您似乎错过了pack
。没有它,界面就不会出现。
fenetre = Tk()
interface = Interface(fenetre)
interface.pack()
interface.mainloop()