This is my program:
import tkinter as tk
import tkinter.font as tkFont
import requests
from tkinter import *
class App:
#### LA BASE ####
def __init__(self, root):
#setting title
root.title("Tkinter News")
#setting window size
width=800
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
GLabel_856=tk.Label(root)
GLabel_856["bg"] = "#d9b8ff"
ft = tkFont.Font(family='Times',size=10)
GLabel_856["font"] = ft
GLabel_856["fg"] = "#d9b8ff"
GLabel_856["justify"] = "center"
GLabel_856["text"] = ""
GLabel_856.place(x=0,y=40,width=800,height=454)
GLabel_339=tk.Label(root)
GLabel_339["bg"] = "#36264e"
ft = tkFont.Font(family='Times',size=38)
GLabel_339["font"] = ft
GLabel_339["fg"] = "#be9ce6"
GLabel_339["justify"] = "center"
GLabel_339["text"] = "Tkinter News"
GLabel_339["relief"] = "raised"
GLabel_339.place(x=0,y=0,width=800,height=80)
GLabel_944=tk.Label(root)
GLabel_944["bg"] = "#1c1125"
ft = tkFont.Font(family='Times',size=10)
GLabel_944["font"] = ft
GLabel_944["fg"] = "#333333"
GLabel_944["justify"] = "center"
GLabel_944["text"] = ""
GLabel_944.place(x=0,y=80,width=800,height=10)
GLabel_580=tk.Label(root)
GLabel_580["bg"] = "#c4a1f8"
ft = tkFont.Font(family='Times',size=10)
GLabel_580["font"] = ft
GLabel_580["fg"] = "#333333"
GLabel_580["justify"] = "center"
GLabel_580["text"] = ""
GLabel_580["relief"] = "sunken"
GLabel_580.place(x=10,y=100,width=385,height=380)
GLabel_129=tk.Label(root)
GLabel_129["bg"] = "#c7b9ff"
ft = tkFont.Font(family='Times',size=10)
GLabel_129["font"] = ft
GLabel_129["fg"] = "#333333"
GLabel_129["justify"] = "center"
GLabel_129["text"] = ""
GLabel_129["relief"] = "sunken"
GLabel_129.place(x=410,y=100,width=385,height=380)
GLabel_177=tk.Label(root)
GLabel_177["bg"] = "#000000"
ft = tkFont.Font(family='Times',size=10)
GLabel_177["font"] = ft
GLabel_177["fg"] = "#333333"
GLabel_177["justify"] = "center"
GLabel_177["text"] = ""
GLabel_177.place(x=0,y=490,width=800,height=10)
GButton_465=tk.Button(root)
GButton_465["bg"] = "#fcbdbd"
ft = tkFont.Font(family='Times',size=13)
GButton_465["font"] = ft
GButton_465["fg"] = "#000000"
GButton_465["justify"] = "center"
GButton_465["text"] = "Start"
GButton_465.place(x=20,y=110,width=70,height=25)
GButton_465["command"] = self.GButton_465_command
#### APRES START ####
def GButton_465_command(self):
print("Start")
GLineEdit_166=tk.Text(root)
GLineEdit_166["borderwidth"] = "1px"
ft = tkFont.Font(family='Times',size=10)
GLineEdit_166["font"] = ft
GLineEdit_166["fg"] = "#333333"
GLineEdit_166.place(x=150,y=140,width=88,height=30)
GLabel_858=tk.Label(root)
GLabel_858["bg"] = "#f491ff"
ft = tkFont.Font(family='Times',size=13)
GLabel_858["font"] = ft
GLabel_858["fg"] = "#333333"
GLabel_858["justify"] = "center"
GLabel_858["text"] = "Filtre"
GLabel_858.place(x=110,y=110,width=216,height=25)
GLabel_708=tk.Label(root)
GLabel_708["bg"] = "#eedbff"
ft = tkFont.Font(family='Times',size=15)
GLabel_708["font"] = ft
GLabel_708["fg"] = "#333333"
GLabel_708["justify"] = "center"
GLabel_708["text"] = "Source"
GLabel_708.place(x=20,y=140,width=120,height=30)
GButton_669=tk.Button(root)
GButton_669["bg"] = "#eedbff"
ft = tkFont.Font(family='Times',size=10)
GButton_669["font"] = ft
GButton_669["fg"] = "#000000"
GButton_669["justify"] = "center"
GButton_669["text"] = "Sources"
GButton_669.place(x=20,y=450,width=70,height=20)
GButton_669["command"] = self.GButton_669_command
GButton_499=tk.Button(root)
GButton_499["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
GButton_499["font"] = ft
GButton_499["fg"] = "#000000"
GButton_499["justify"] = "center"
GButton_499["text"] = "Categories "
GButton_499.place(x=100,y=450,width=70,height=20)
GButton_499["command"] = self.GButton_499_command
GLabel_233=tk.Label(root)
GLabel_233["activebackground"] = "#ba8cd9"
GLabel_233["bg"] = "#f491ff"
ft = tkFont.Font(family='Times',size=13)
GLabel_233["font"] = ft
GLabel_233["fg"] = "#333333"
GLabel_233["justify"] = "center"
GLabel_233["text"] = "Disponibilité"
GLabel_233.place(x=30,y=410,width=207,height=30)
GButton_362=tk.Button(root)
GButton_362["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
GButton_362["font"] = ft
GButton_362["fg"] = "#000000"
GButton_362["justify"] = "center"
GButton_362["text"] = "Pays"
GButton_362.place(x=180,y=450,width=70,height=20)
GButton_362["command"] = self.GButton_362_command
GButton_462=tk.Button(root)
GButton_462["bg"] = "#f0f0f0"
ft = tkFont.Font(family='Times',size=10)
GButton_462["font"] = ft
GButton_462["fg"] = "#000000"
GButton_462["justify"] = "center"
GButton_462["text"] = "Langues"
GButton_462.place(x=260,y=450,width=70,height=20)
GButton_462["command"] = self.GButton_462_command
def Source_textbox_input():
SourceBox=GLineEdit_166.get("1.0","end-1c")
print(SourceBox)
# buttonCommit=Button(root,width=10,height=1, text="Valider",command=lambda: retrieve_input())
# buttonCommit.pack()
GButton_491=tk.Button(root)
GButton_491["bg"] = "#eedbff"
ft = tkFont.Font(family='Times',size=10)
GButton_491["font"] = ft
GButton_491["fg"] = "#000000"
GButton_491["justify"] = "center"
GButton_491["text"] = "Valider"
GButton_491.place(x=250,y=140,width=68,height=30)
GButton_491["command"] = lambda: Source_textbox_input()
GButton_412=tk.Button(root)
GButton_412["bg"] = "#eedbff"
ft = tkFont.Font(family='Times',size=10)
GButton_412["font"] = ft
GButton_412["fg"] = "#000000"
GButton_412["justify"] = "center"
GButton_412["text"] = "Afficher"
GButton_412.place(x=322,y=140,width=68,height=30)
GButton_412["command"] = self.GButton_412_command
def GButton_412_command(self):
print("afficher")
def filtrer_par_source(nom): #FILTRE ARTICLE PAR SOURCE NOM
"""
fonction qui filtre les recherches selon la source choisis
"""
rien = ('https://newsapi.org/v2/top-headlines?sources=,&br&apiKey=67d4540bfb764bc69b76e6f65298ab1f')
listeurl = rien.split(',')
#print(listeurl)
url = str(listeurl[0] + nom + listeurl[1])
#print(url)
rep = requests.get(url)
print(rep.json())
filtrer_par_source(SourceBox)
#### Fonction des boutons ####
############################# Disponible ##############################
def GButton_669_command(self):
print("Source")
def sources_dispo(): #AFFICHE LES SOURCES DISPONIBLES
"""
fonction qui retourne le nom de toutes les sources disponibles
"""
url = ('https://newsapi.org/v2/top-headlines/sources?'
'apiKey=67d4540bfb764bc69b76e6f65298ab1f')
liste_source = []
rep = requests.get(url)
for i in rep.json()['sources']:
liste_source.append(i['name'])
return liste_source
#print(sources_dispo())
#Afficher sources_dispo sur Tkinter
GMessage_941=tk.Message(root)
ft = tkFont.Font(family='Times',size=9)
GMessage_941["font"] = ft
GMessage_941["fg"] = "#333333"
GMessage_941["justify"] = "center"
GMessage_941["text"] = sources_dispo()
GMessage_941.place(x=420,y=110,width=370,height=360)
def GButton_499_command(self):
print("Category")
def category_dispo(): #AFFICHE LES CATEGORIES DISPONIBLES
"""
fonction qui retourne toutes les categories disponibles
"""
url = ('https://newsapi.org/v2/top-headlines/sources?'
'apiKey=67d4540bfb764bc69b76e6f65298ab1f')
liste_cat = []
rep = requests.get(url)
for i in rep.json()['sources']:
liste_cat.append(i['category'])#ajout dans l'ensemble
liste_cat = list(set(liste_cat))
return liste_cat
#Afficher category sur Tkinter
GMessage_941=tk.Message(root)
ft = tkFont.Font(family='Times',size=15)
GMessage_941["font"] = ft
GMessage_941["fg"] = "#333333"
GMessage_941["justify"] = "center"
GMessage_941["text"] = category_dispo()
GMessage_941.place(x=420,y=110,width=370,height=360)
def GButton_362_command(self):
print("Pays")
def pays_dispo(): #AFFICHE LES PAYS DISPONIBLE
"""
fonction qui retourne les pays disponibles
"""
url = ('https://newsapi.org/v2/top-headlines/sources?'
'apiKey=67d4540bfb764bc69b76e6f65298ab1f')
liste_pays = []
rep = requests.get(url)
for j in rep.json()['sources']:
liste_pays.append(j['country'])
liste_pays = list(set(liste_pays))
return liste_pays
#Afficher pays sur Tkinter
GMessage_941=tk.Message(root)
ft = tkFont.Font(family='Times',size=15)
GMessage_941["font"] = ft
GMessage_941["fg"] = "#333333"
GMessage_941["justify"] = "center"
GMessage_941["text"] = pays_dispo()
GMessage_941.place(x=420,y=110,width=370,height=360)
def GButton_462_command(self):
print("langues")
def langues_dispo(): #AFFICHE LES LANGUES DISPONIBLES
"""
fonction qui retourne toutes les langues disponibles
"""
url = ('https://newsapi.org/v2/top-headlines/sources?'
'apiKey=67d4540bfb764bc69b76e6f65298ab1f')
liste_langues = []
rep = requests.get(url)
for i in rep.json()['sources']:
liste_langues.append(i['language'])
liste_langues = list(set(liste_langues))
return liste_langues
#print(langues_dispo())
#Afficher langue sur Tkinter
GMessage_941=tk.Message(root)
ft = tkFont.Font(family='Times',size=15)
GMessage_941["font"] = ft
GMessage_941["fg"] = "#333333"
GMessage_941["justify"] = "center"
GMessage_941["text"] = langues_dispo()
GMessage_941.place(x=420,y=110,width=370,height=360)
############################## filtre ####################################
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
So it's a program that will return the source of an API news website And we also have to make a filter system
It tells me NameError: name 'SourceBox'
is not defined
Can anyone help me find the right place to put def Source_textbox_input():
So I tried to move the function several times but it gave me error messages each time
As written, SourceBox
is local to the method Source_textbox_input()
. Do the following to make SourceBox
available to all the methods in your App
class
# add 'self.SourceBox' to the __init__ method
class App:
def __init__(self, root):
...
# not strictly necessary, but prevents issues if something tries to access
# 'SourceBox` before it's created
self.SourceBox = None # default value
Modify the Source_textbox_input()
function so it can access 'self'
def Source_textbox_input(self):
self.SourceBox=GLineEdit_166.get("1.0","end-1c")
print(self.SourceBox)
Update the lambda bound to Source_textbox_input
GButton_491["command"] = lambda: Source_textbox_input(self)
And finally, you can now do this
filtrer_par_source(self.SourceBox)
In the future, please refer to this guide on how to provide a minimal reproducible example , and read about how to ask . This is far too much code.
And as an aside, refer to PEP 8, the Python Style Guide - your naming convention for variables and methods / functions goes against Python style practices.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.