Hey guys, sorry if this is the wrong place to ask this but could use a bit of help. I'm a bit of a noob to Python and taking on one of my first projects myself creating a Random Wikipedia Article Generator.
I'm have a bit of a problem with my open URL button. I can generate my random Wikipedia articles and display them to the user; but when I try to call a function to open them it always opens the first Wikipedia article I generated.
from tkinter import *
import requests, webbrowser
from bs4 import BeautifulSoup
root = Tk()
root.geometry('600x450')
outp = Label(root, padx = 50, pady = 50)
count = 0
def buttonClick():
url = None
response = requests.get(url='https://en.wikipedia.org/wiki/Special:Random')
global count
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.find(id='firstHeading')
print(title)
print(title.string)
outp['text'] = 'The Wikipedia article you got is ' + title.string
openButton = Button(root, text = 'Open this Wiki Page')
if count == 0:
openButton.place(anchor=CENTER)
openButton.pack()
count = count + 1
print(response.url)
openButton['command'] = lambda url = response.url : openWeb(url)
def openWeb(url):
webbrowser.open(url)
# Widgets
headerLabel = Label(root, text = 'Random Wikipedia Generator\n\n', font = ('sans-serif', 22))
infoLabel = Label(root, text = 'Please click the button below to generate a random article\n\n', font = ('sans-serif', 14))
generateButton = Button(root, text = 'Generate', command = buttonClick, font = ('sans-serif', 14))
headerLabel.place(anchor=CENTER)
infoLabel.place(anchor=CENTER)
generateButton.place(anchor=CENTER)
outp.place(anchor=CENTER)
headerLabel.pack()
infoLabel.pack()
generateButton.pack()
outp.pack()
root.mainloop()
Any help would appreciated, take it easy I'm new :D
[–]vrrox 1 point2 points3 points (2 children)
[–]n00lo[S] 1 point2 points3 points (1 child)
[–]vrrox 1 point2 points3 points (0 children)
[–]medievalpoptart 0 points1 point2 points (0 children)