I have created a simple program (Python, Selenium, w/ RPA framework) that takes people names and automatically outputs people's data from wikipedia. Recently I also added basic tkinter scripts to visualize the output. At first it was working, but now it's just opening a Tkinter window with empty black screen (See photos with captions)
Here's my code setup:
In my robotics.py:
from tkinter.ttk import Frame
from tkinter import ttk
import tkinter as tk
from datetime import datetime
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import os
os.environ['TK_SILENCE_DEPRECATION'] = '1'
SCIENTISTS = ["Albert Einstein", "Isaac Newton",
"Marie Curie", "Charles Darwin"]
scientists_info_list = []
class Robot:
def __init__(self, name):
self.name = name
def say_hello(self):
print("Hello, my name is " + self.name)
def say_goodbye(self):
print("Goodbye, my name is " + self.name)
def fetch_scientists_on_wikipidea(self):
driver = webdriver.Chrome()
for scientist in SCIENTISTS:
# navigate to wikipedia page
driver.get(
f"https://en.wikipedia.org/wiki/{scientist.replace(' ', '_')}")
try:
# wait for the birthDate element to be visible
birth_date_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "span.bday")))
birth_date = birth_date_element.get_attribute("innerText")
death_date_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, "//th[contains(text(), 'Died')]/following-sibling::td//span"))
)
death_date = death_date_element.get_attribute(
"innerText").replace("(", "").replace(")", "")
born = datetime.strptime(birth_date, '%Y-%m-%d')
died = datetime.strptime(death_date, '%Y-%m-%d')
age = int((died - born).days / 365.25)
summary = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, "(//div[@id='mw-content-text']/div/p[not(@class='mw-empty-elt')])[1]"))
).text
# store information in dictionary
scientist_info = {
"Name": scientist,
"Born": birth_date,
"Died": death_date,
"Age": age,
"Summary": summary
}
scientists_info_list.append(scientist_info)
# display information
for key, value in scientist_info.items():
print(f"{key}: {value}")
sleep(1)
except TimeoutException as e:
print(f"An error occurred while processing {scientist}: {e}")
# Close the browser after the loop
driver.quit()
display_scientists_info(scientists_info_list)
class ScrollableFrame(ttk.Frame):
def __init__(self, container, bg_color, *args, **kwargs):
super().__init__(container, *args, **kwargs)
self.canvas = tk.Canvas(self, bg=bg_color, width=350, height=150)
self.scrollbar = ttk.Scrollbar(
self, orient="vertical", command=self.canvas.yview)
self.scrollable_frame = ttk.Frame(self.canvas)
self.scrollable_frame.bind("<Configure>", lambda e: self.canvas.configure(
scrollregion=self.canvas.bbox("all")))
self.canvas.create_window(
(0, 0), window=self.scrollable_frame, anchor="nw")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side="left", fill="both")
self.scrollbar.pack(side="right", fill="y")
def display_scientists_info(scientists_info_list):
window = tk.Tk()
window.title("Scientist Information")
intro_label = tk.Label(
window, text="Hello, my name is WikiRobot", font=("Arial", 16))
intro_label.pack(pady=10)
colors = ["#072F38", "#334E3B"]
for index, scientist_info in enumerate(scientists_info_list):
color = colors[index % 2]
outer_frame = tk.Frame(window, bd=2, relief="groove",
highlightbackground="#C8CBC0", highlightthickness=1)
outer_frame.pack(padx=10, pady=10)
inner_frame = ScrollableFrame(outer_frame, bg_color=color)
inner_frame.pack(padx=5, pady=5)
for key, value in scientist_info.items():
if key != "Age":
label = tk.Label(inner_frame.scrollable_frame,
text=f"{key}: {value}", foreground="white", bg=inner_frame.scrollable_frame['bg'], wraplength=1240-30)
label.pack(padx=5, pady=5, fill="both", expand=True)
window.mainloop()
class ScrollableFrame(tk.Frame):
def __init__(self, container, bg_color=None, width=1240, height=170, *args, **kwargs):
super().__init__(container, *args, **kwargs)
self.configure(width=width)
self.canvas = tk.Canvas(self, bg=bg_color, width=width, height=height)
self.scrollbar = ttk.Scrollbar(
self, orient="vertical", command=self.canvas.yview)
self.scrollable_frame = tk.Frame(self.canvas, bg=bg_color)
self.scrollable_frame.configure(width=width)
self.scrollable_frame.bind("<Configure>", lambda e: self.canvas.configure(
scrollregion=self.canvas.bbox("all")))
self.canvas.create_window(
(0, 0), window=self.scrollable_frame, anchor="nw")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side="left", fill="both", expand=True)
self.scrollbar.pack(side="right", fill="y")
In my main.py:
from robotics import Robot
robot = Robot("WikiRobot")
def introduce_yourself():
robot.say_hello()
def quit_robot():
robot.say_goodbye()
def main():
introduce_yourself()
robot.fetch_scientists_on_wikipidea()
quit_robot()
if __name__ == "__main__":
main()
I am receiving the desired output in the terminal:
Hello, my name is WikiRobot
Name: Albert Einstein
Born: 1879-03-14
Died: 1955-04-18
Age: 76
Summary: Albert Einstein (/ˈaɪnstaɪn/ EYEN-styne;[4] German: [ˈalbɛʁt ˈʔaɪnʃtaɪn] (listen); 14 March 1879 – 18 April 1955) was a German-born theoretical physicist,[5] widely acknowledged to be one of the greatest and most influential physicists of all time. Best known for developing the theory of relativity, he also made important contributions to the development of the theory of quantum mechanics. Relativity and quantum mechanics are the two pillars of modern physics.[1][6] His mass–energy equivalence formula E = mc2, which arises from relativity theory, has been dubbed "the world's most famous equation".[7] His work is also known for its influence on the philosophy of science.[8][9] He received the 1921 Nobel Prize in Physics "for his services to theoretical physics, and especially for his discovery of the law of the photoelectric effect",[10] a pivotal step in the development of quantum theory. His intellectual achievements and originality resulted in "Einstein" becoming synonymous with "genius".[11] Einsteinium, one of the synthetic elements in the periodic table, was named in his honor.[12]
Name: Isaac Newton
Born: 1643-01-04
Died: 1727-03-31
Age: 84
Summary: Sir Isaac Newton FRS (25 December 1642 – 20 March 1726/27)[a] was an English mathematician, physicist, astronomer, alchemist, theologian, and author who was described in his time as a natural philosopher. He was a key figure in the Scientific Revolution and the Enlightenment that followed. His pioneering book Philosophiæ Naturalis Principia Mathematica (Mathematical Principles of Natural Philosophy), first published in 1687, consolidated many previous results and established classical mechanics.[17][18] Newton also made seminal contributions to optics, and shares credit with German mathematician Gottfried Wilhelm Leibniz for developing infinitesimal calculus.
Name: Marie Curie
Born: 1867-11-07
Died: 1934-07-04
Age: 66
Summary: Marie Salomea Skłodowska–Curie (/ˈkjʊəri/ KURE-ee,[4] French pronunciation: [maʁi kyʁi], Polish pronunciation: [ˈmarja skwɔˈdɔfska kʲiˈri]; born Maria Salomea Skłodowska, Polish: [ˈmarja salɔˈmɛa skwɔˈdɔfska]; 7 November 1867 – 4 July 1934) was a Polish and naturalized-French physicist and chemist who conducted pioneering research on radioactivity. She was the first woman to win a Nobel Prize, the first person to win a Nobel Prize twice, and the only person to win a Nobel Prize in two scientific fields. Her husband, Pierre Curie, was a co-winner of her first Nobel Prize, making them the first-ever married couple to win the Nobel Prize and launching the Curie family legacy of five Nobel Prizes. She was, in 1906, the first woman to become a professor at the University of Paris.[5]
Name: Charles Darwin
Born: 1809-02-12
Died: 1882-04-19
Age: 73
Summary: Charles Robert Darwin FRS FRGS FLS FZS JP[6] (/ˈdɑːrwɪn/[7] DAR-win; 12 February 1809 – 19 April 1882) was an English naturalist, geologist, and biologist,[8] widely known for his contributions to evolutionary biology. His proposition that all species of life have descended from a common ancestor is now generally accepted and considered a fundamental concept in science.[9] In a joint publication with Alfred Russel Wallace, he introduced his scientific theory that this branching pattern of evolution resulted from a process he called natural selection, in which the struggle for existence has a similar effect to the artificial selection involved in selective breeding.[10] Darwin has been described as one of the most influential figures in human history and was honoured by burial in Westminster Abbey.[11][12]
Goodbye, my name is Quandrinaut
(base) sam@Samsons-MacBook-Pro robotic_researcher_ordonez-iter2 % python main.py
Hello, my name is Quandrinaut
Name: Albert Einstein
Born: 1879-03-14
Died: 1955-04-18
Age: 76
Summary: Albert Einstein (/ˈaɪnstaɪn/ EYEN-styne;[4] German: [ˈalbɛʁt ˈʔaɪnʃtaɪn] (listen); 14 March 1879 – 18 April 1955) was a German-born theoretical physicist,[5] widely acknowledged to be one of the greatest and most influential physicists of all time. Best known for developing the theory of relativity, he also made important contributions to the development of the theory of quantum mechanics. Relativity and quantum mechanics are the two pillars of modern physics.[1][6] His mass–energy equivalence formula E = mc2, which arises from relativity theory, has been dubbed "the world's most famous equation".[7] His work is also known for its influence on the philosophy of science.[8][9] He received the 1921 Nobel Prize in Physics "for his services to theoretical physics, and especially for his discovery of the law of the photoelectric effect",[10] a pivotal step in the development of quantum theory. His intellectual achievements and originality resulted in "Einstein" becoming synonymous with "genius".[11] Einsteinium, one of the synthetic elements in the periodic table, was named in his honor.[12]
Name: Isaac Newton
Born: 1643-01-04
Died: 1727-03-31
Age: 84
Summary: Sir Isaac Newton FRS (25 December 1642 – 20 March 1726/27)[a] was an English mathematician, physicist, astronomer, alchemist, theologian, and author who was described in his time as a natural philosopher. He was a key figure in the Scientific Revolution and the Enlightenment that followed. His pioneering book Philosophiæ Naturalis Principia Mathematica (Mathematical Principles of Natural Philosophy), first published in 1687, consolidated many previous results and established classical mechanics.[17][18] Newton also made seminal contributions to optics, and shares credit with German mathematician Gottfried Wilhelm Leibniz for developing infinitesimal calculus.
Name: Marie Curie
Born: 1867-11-07
Died: 1934-07-04
Age: 66
Summary: Marie Salomea Skłodowska–Curie (/ˈkjʊəri/ KURE-ee,[4] French pronunciation: [maʁi kyʁi], Polish pronunciation: [ˈmarja skwɔˈdɔfska kʲiˈri]; born Maria Salomea Skłodowska, Polish: [ˈmarja salɔˈmɛa skwɔˈdɔfska]; 7 November 1867 – 4 July 1934) was a Polish and naturalized-French physicist and chemist who conducted pioneering research on radioactivity. She was the first woman to win a Nobel Prize, the first person to win a Nobel Prize twice, and the only person to win a Nobel Prize in two scientific fields. Her husband, Pierre Curie, was a co-winner of her first Nobel Prize, making them the first-ever married couple to win the Nobel Prize and launching the Curie family legacy of five Nobel Prizes. She was, in 1906, the first woman to become a professor at the University of Paris.[5]
Name: Charles Darwin
Born: 1809-02-12
Died: 1882-04-19
Age: 73
Summary: Charles Robert Darwin FRS FRGS FLS FZS JP[6] (/ˈdɑːrwɪn/[7] DAR-win; 12 February 1809 – 19 April 1882) was an English naturalist, geologist, and biologist,[8] widely known for his contributions to evolutionary biology. His proposition that all species of life have descended from a common ancestor is now generally accepted and considered a fundamental concept in science.[9] In a joint publication with Alfred Russel Wallace, he introduced his scientific theory that this branching pattern of evolution resulted from a process he called natural selection, in which the struggle for existence has a similar effect to the artificial selection involved in selective breeding.[10] Darwin has been described as one of the most influential figures in human history and was honoured by burial in Westminster Abbey.[11][12]
enter image description here I managed to make the Tkinter work before as per my code:
enter image description here But going back to it few hrs later, this is what I'm getting. The output in the terminal is still successfully printing
I tried googling for solutions, I found tips like updating Tkinter be uninstalling python and reinstalling it? Appreciate the help