integration interface graphique dans objet

This commit is contained in:
vincent 2018-01-24 19:36:27 +01:00
parent e129a0643c
commit 4cb0c6028e

View File

@ -1,26 +1,65 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import * from tkinter import *
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import urllib.request import urllib.request
import json import json
import sys import sys
def click(): class Interface:
print(value.get())
try:
label2["text"] += data[value.get()]+"\r"
print(data[value.get()])
except KeyError:
print("numero de chaine inconnue")
label2["text"] += "numero de chaine inconnue"+"\r"
value.set("")
def click_reset(): def __init__(self):
print("exit") self.data = load_jsonfile('chaine.json')
fenetre.quit self.fenetre = Tk()
label2["text"] = "" self.fenetre.title("recherche de chaine")
fenetre.mainloop self.value = StringVar()
self.label = Label(self.fenetre, text="entrer numero de chaine")
self.entree = Entry(self.fenetre, textvariable=self.value, width=30)
self.frame = Frame(self.fenetre)
self.label2 = Label(self.fenetre, text="")
self.bouton_update_base = Button(
self.fenetre, text="update la base de chaine", command=self.click_update)
self.bouton = Button(self.frame, text="OK", command=self.click)
self.reset = Button(self.frame, text="reset", command=self.click_reset)
self.label.pack()
self.entree.pack()
self.entree.focus_set()
self.frame.pack()
self.bouton.pack(side=LEFT)
self.reset.pack(side=RIGHT)
self.label2.pack()
self.bouton_update_base.pack()
self.fenetre.bind("<Key-Return>", self.enter)
self.fenetre.bind("<Key-Escape>", self.eventreset)
def enter(self,evt):
self.click()
def eventreset(self,evt):
self.click_reset()
def mainloop(self):
self.fenetre.mainloop()
def click(self):
print(self.value.get())
try:
self.label2["text"] += self.data[self.value.get()]+"\r"
print(self.data[self.value.get()])
except KeyError:
print("numero de chaine inconnue")
self.label2["text"] += "numero de chaine inconnue"+"\r"
self.value.set("")
def click_reset(self):
print("exit")
self.fenetre.quit
self.label2["text"] = ""
self.fenetre.mainloop
def click_update(self):
parsechaine()
self.data = load_jsonfile('chaine.json')
self.label2["text"] += "update chaine done"+"\r"
@ -36,6 +75,8 @@ def RepresentsInt(s):
return False return False
except TypeError: except TypeError:
return False return False
def parsechaine(): def parsechaine():
URL = 'https://fr.wikipedia.org/wiki/Liste_des_cha%C3%AEnes_de_Canal' URL = 'https://fr.wikipedia.org/wiki/Liste_des_cha%C3%AEnes_de_Canal'
liste_chaine = {} liste_chaine = {}
@ -59,50 +100,22 @@ def parsechaine():
print(json.dumps(liste_chaine, indent=4)) print(json.dumps(liste_chaine, indent=4))
with open('chaine.json', 'w', encoding='utf-8') as f: with open('chaine.json', 'w', encoding='utf-8') as f:
json.dump(liste_chaine, f, indent=4) json.dump(liste_chaine, f, indent=4)
data = load_jsonfile('chaine.json')
label2["text"] += "update chaine done"+"\r"
def enter(evt):
click()
def eventreset(evt):
click_reset()
data = load_jsonfile('chaine.json')
fenetre = Tk()
fenetre.title("recherche de chaine")
value = StringVar()
interface = Interface()
if len(sys.argv) > 1: if len(sys.argv) > 1:
arg = True arg = True
else: else:
value.set("") interface.value.set("")
arg = False arg = False
label = Label(fenetre, text="entrer numero de chaine")
entree = Entry(fenetre, textvariable=value, width=30)
frame = Frame(fenetre)
label2 = Label(fenetre, text="")
bouton_update_base = Button(fenetre, text="update la base de chaine", command=parsechaine)
bouton = Button(frame, text="OK", command=click)
reset = Button(frame, text="reset", command=click_reset)
label.pack()
entree.pack()
entree.focus_set()
frame.pack()
bouton.pack(side=LEFT)
reset.pack(side=RIGHT)
label2.pack()
bouton_update_base.pack()
fenetre.bind("<Key-Return>", enter)
fenetre.bind("<Key-Escape>", eventreset)
if arg == True: if arg == True:
for i in sys.argv[1:]: for i in sys.argv[1:]:
value.set(i) interface.value.set(i)
click() interface.click()
value.set("") interface.value.set("")
fenetre.mainloop() interface.mainloop()