Witam wszystkich, to mój pierwszy post więc mam nadzieje że wstawiam wszystko w odpowiednie miejsce.
Piszę z prośbą o pomoc. Piszę aktualnie swój pierwszy projekt w Pythonie używając frameworka PyQ5t i biblioteki matplotlib, ale problem dotyczy samego działania PyQt5. Apka ma proste działanie, odczytywanie i pobieranie danych z netu w formacie JSON i CSV i na ich podstawie wyświetlanie wykresów itp. Aktualnie mam napisane pierwsze okno, w którym wybiera się co chce się wyświetlić (aktualnie tylko jedna opcja dotycząca wyświetlania w postaci wykresu danych pobranych z GitHuba). Wyboru dokonuje się przez naciśnięcie przycisku. Po naciśnięciu przycisku okno się odświeża i wyświetlają się kolejne przyciski, dzięki którym użytkownik ma wybierać jaki język programowania go interesuje. Wszystko działało w porządku do momentu gdy do drugiego okna (tego z wyborem języków programowania) dodałem metody obsługujące wciskanie przycisków. Cała apka się wysypuje i samoistnie wyświetla jeden wykres. Wstawiam kod poniżej, z góry dziękuje za pomoc ;)
import sys
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication , QMainWindow, QWidget, QLabel, QGridLayout
from StatisticsFromGitHub import GitHubCharts
class LanguagesView(QWidget):
"""Window settings where user can select a language to view its graph"""
def __init__(self, parent=None):
"""Settings initializations"""
super(LanguagesView, self).__init__(parent)
#Texts settings
message_first = "Which language do you want to search?"
self.text_1 = QLabel(self)
self.text_1.setText(message_first)
self.text_1.move(290, 0)
self.text_1.setFont(QtGui.QFont("Times", 15))
self.text_1.resize(self.text_1.sizeHint())
#Buttons generate
self.button_1 = QtWidgets.QPushButton(self)
self.button_1.setText("Python")
self.button_2 = QtWidgets.QPushButton(self)
self.button_2.setText("C")
self.button_3 = QtWidgets.QPushButton(self)
self.button_3.setText("C++")
self.button_4 = QtWidgets.QPushButton(self)
self.button_4.setText("C#")
self.button_5 = QtWidgets.QPushButton(self)
self.button_5.setText("Java")
self.button_6 = QtWidgets.QPushButton(self)
self.button_6.setText("JavaScript")
self.button_7 = QtWidgets.QPushButton(self)
self.button_7.setText("PHP")
self.button_8 = QtWidgets.QPushButton(self)
self.button_8.setText("SQL")
self.button_9 = QtWidgets.QPushButton(self)
self.button_9.setText("Back")
#Buttons layout settings
ukladT = QGridLayout()
ukladT.addWidget(self.button_1, 1, 0)
ukladT.addWidget(self.button_2, 1, 1)
ukladT.addWidget(self.button_3, 2, 0)
ukladT.addWidget(self.button_4, 2, 1)
ukladT.addWidget(self.button_5, 3, 0)
ukladT.addWidget(self.button_6, 3, 1)
ukladT.addWidget(self.button_7, 4, 0)
ukladT.addWidget(self.button_8, 4, 1)
ukladT.addWidget(self.button_9, 5, 0, 1, 2)
self.setLayout(ukladT)
class FirstMenu(QWidget):
"""First window settings"""
def __init__(self, parent=None):
"""Settings initialization"""
super(FirstMenu, self).__init__(parent)
message_first = "This application's aim is show some charts and something else" \
"\nPress the button and display what you want"
message_GitHub = "1. Press button 'GitHub' to show the best projects in diffrent languages from GitHub:"
self.text_1 = QLabel(self)
self.text_1.setText(message_first)
self.text_1.move(200, 0)
self.text_1.setFont(QtGui.QFont("Times", 15))
self.text_2 = QtWidgets.QLabel(self)
self.text_2.setText(message_GitHub)
self.text_2.move(0, 100)
self.text_2.setFont(QtGui.QFont("Times", 10))
self.button = QtWidgets.QPushButton(self)
self.button.setText("GitHub")
self.button.move(600, 95)
class MainWindow(QMainWindow):
"""Application window settings"""
def __init__(self, parent=None):
"""Settings initialization"""
super(MainWindow, self).__init__(parent)
self.setGeometry(300, 300, 300, 300)
self.setFixedSize(1000, 600)
self.setWindowTitle("DisplayFiguresAndMaps")
self.startUI()
def startUI(self):
"""Display first window in application"""
self.nowy = FirstMenu(self)
self.setCentralWidget(self.nowy)
self.nowy.button.clicked.connect(self.clicked_Git_Hub)
self.show()
def clicked_Git_Hub(self):
"""Go to window where user can select which language statistics wants to see"""
self.nowy2 = LanguagesView()
self.setCentralWidget(self.nowy2)
self.nowy2.button_2.clicked.connect(self.clicked_c())
self.nowy2.button_3.clicked.connect(self.clicked_cpp())
self.nowy2.button_4.clicked.connect(self.clicked_c_sharp())
self.nowy2.button_5.clicked.connect(self.clicked_java())
self.nowy2.button_6.clicked.connect(self.clicked_java_script())
self.nowy2.button_7.clicked.connect(self.clicked_php())
self.nowy2.button_8.clicked.connect(self.clicked_sql())
self.nowy2.button_9.clicked.connect(self.clicked_back())
self.show()
def clicked_python(self):
python_chart = GitHubCharts('python')
self.show()
def clicked_c(self):
c_chart = GitHubCharts('c')
self.show()
def clicked_cpp(self):
cpp_chart = GitHubCharts('cpp')
self.show()
def clicked_c_sharp(self):
c_sharp_chart = GitHubCharts('c#')
self.show()
def clicked_java(self):
java_chart = GitHubCharts('java')
self.show()
def clicked_java_script(self):
java_script_chart = GitHubCharts('javascript')
self.show()
def clicked_php(self):
php_chart = GitHubCharts('php')
self.show()
def clicked_sql(self):
sql_chart = GitHubCharts('sql')
self.show()
def clicked_back(self):
self.startUI()
if __name__ == '__main__':
"""Start the application"""
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
#####################################
#####################################
#Drugi moduł programu rysujący wykresy
import requests
from plotly import offline
class GitHubCharts():
"""Class is responsible for drawing a Bar graph with datas about languages from GitHub"""
def __init__(self, language):
"""Reading datas from URL and initialize variables"""
#Creating variables
self.repo_links, self.stars, self.labels = [], [], []
self.my_language = language
#Page details and read datas
URL = f"https://api.github.com/search/repositories?q=language:{language}&sort=star"
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(URL, headers=headers)
response_dict = r.json()
self.response_dicts = response_dict['items']
#self.write_datas(self.response_dicts)
def write_datas(self, response_dicts):
"""Write datas from page to variables"""
for repo_dict in response_dicts:
repo_name = repo_dict['name']
repo_url = repo_dict['html_url']
self.repo_links.append(f"<a href='{repo_url}'>{repo_name}</a>")
self.stars.append(repo_dict['stargazers_count'])
owner = repo_dict['owner']['login']
description = repo_dict['description']
self.labels.append(f"{owner}<br />{description}")
self.bar_settings()
def bar_settings(self):
"""Bar graph settings"""
data = [{
'type': 'bar',
'x': self.repo_links,
'y': self.stars,
'hovertext': self.labels,
'marker': {
'color': '#4682B4',
'line': {'width': 1.5, 'color': '#778899'},
'opacity': 0.8,
},
}]
my_layout = {'title': {
'text': f"{self.my_language.title()} projects with the highest number of stars on GitHub.\n"
f" You can press on project name to go to the project page",
'font': {
'family': 'Times New Roman',
'size': 25
},
'x': 0.5,
},
'xaxis': {
'title': 'Repository',
'titlefont': {'size': 25},
'tickfont': {'size': 15},
},
'yaxis': {
'title': 'Amount of stars',
'titlefont': {'size': 25},
'tickfont': {'size': 15},
},
}
self.draw(data, my_layout)
def draw(self, data, my_layout):
"""Drawing Bar graph"""
fig = {'data': data, "layout": my_layout}
offline.plot(fig, filename=f"data/{self.my_language.lower()}_GitHub.html")