I’m trying to create a display of local world panels that people can upload to my database. Each of these local worlds has different data, and it’s precisely this data that distinguishes them. Each world is displayed on a panel, but I’m facing an issue: only the last world from the database is being displayed. I know this is because each panel is stored in the same variable. I don’t know how to implement the functionality I described earlier. I know how to do this in customtkinter; I even have a snippet of such code, but PySide6 surprises me.
PySide6 code:
`class JavaWorldPanelsLogic:
def init(self, parent):
self.a = parent
self.count = 1
self.margin = 5
self.favicon_images = []
self.y_position = (96 * self.count) + self.margin
self.panels = []
class JavaWorldPanelsLogic:
def init(self, parent):
self.a = parent
self.count = 1
self.margin = 5
self.favicon_images = []
self.y_position = (96 * self.count) + self.margin
self.panels = []
def load_favicon(self, favicon_b64):
try:
favicon_data = base64.b64decode(favicon_b64.split(',')[1])
self.a.pixmap = QPixmap()
self.a.pixmap.loadFromData(favicon_data)
self.a.favicon = QLabel(parent=self.a.world_panel)
self.a.favicon.setGeometry(12, 12, 51, 51)
self.a.favicon.setPixmap(self.a.pixmap)
self.a.favicon.setScaledContents(True)
except Exception as e:
print(f"Error loading favicon: {e}")
def connect(self):
try:
g = GetConnectionCredentials()
connection = g.connectToDB()
cursor = connection.cursor()
cursor.execute("SELECT * FROM `JavaLocalWorldsList`")
rows = cursor.fetchall()
column_names = [i[0] for i in cursor.description]
cursor.close()
connection.close()
dt = pd.DataFrame(rows, columns=column_names)
return dt
except Exception as e:
print(f"Error: {e}")
return None
def spawnThePanelOfTheWorld(self, ms, id, UserName, PassIsEnabled):
self.a.world_panel = QWidget(parent=self.a.ui.centralwidget)
self.a.world_panel.setStyleSheet("background-color: #141414; border-radius: 5px;")
#self.a.world_panel.setFixedSize(1095, 74)
#self.a.world_panel.move(298, self.y_position)
player_count_text = f"{ms.current_players} из {ms.max_players}"
self.a.player_count_label = QLabel(player_count_text, parent=self.a.world_panel)
self.a.player_count_label.setStyleSheet("color: white;")
self.a.player_count_label.setFont(QFont("Microsoft YaHei", 18))
self.a.player_count_label.setGeometry(729, 0, 200, 74)
self.a.name_of_world_label = QLabel(ms.stripped_motd, parent=self.a.world_panel)
self.a.name_of_world_label.setStyleSheet("color: white;")
self.a.name_of_world_label.setGeometry(69, 12, 176, 20)
password_status = " • С паролем" if PassIsEnabled == "True" else ""
ping_and_version_text = f"• {ms.version} • Пинг {ms.latency} ms • Хост {UserName}{password_status}"
self.a.ping_and_version_label = QLabel(ping_and_version_text, parent=self.a.world_panel)
self.a.ping_and_version_label.setStyleSheet("color: #5A5A5A;")
self.a.ping_and_version_label.setGeometry(70, 44, 400, 20)
self.a.play_button = QPushButton("Скопировать адрес", parent=self.a.world_panel)
self.a.play_button.setStyleSheet("background-color: #253d3d; color: white; border-radius: 10px;")
self.a.play_button.setGeometry(892, 12, 180, 45)
self.a.play_button.setFont(QFont("Microsoft YaHei", 9.75))
self.a.ui.label_2.setText("")
self.a.world_panel.setGeometry(298, self.y_position, 1095, 74) # Adjust the vertical position
self.load_favicon(ms.favicon_b64)
self.count += 1
self.panels.append(self.a.world_panel)
def loadWorlds(self):
dt = self.connect()
if dt is not None:
print(dt.head())
for index, localWorld in dt.iterrows():
ip = localWorld["ip"]
port = localWorld["port"]
id = localWorld["id"]
UserID = localWorld["userID"]
UserName = localWorld["UserName"]
PassIsEnabled = localWorld["PassIsEnabled"]
noOneHostsWorldLabel = None
self.checkWorld(ip, port, id, UserName, PassIsEnabled, noOneHostsWorldLabel)
else:
print("Failed to fetch data from the database.")
def checkWorld(self, ip, port, id, UserName, PassIsEnabled, noOneHostsWorldLabel):
ms = minestat.MineStat(str(ip), int(port))
if ms.online:
self.spawnThePanelOfTheWorld(ms, id, UserName, PassIsEnabled)`
Customtkinter code:
`count = 1
margin = 5
class Favicon:
def init(self):
self.favicon_images = []
def load_favicon(self, favicon_b64, worldPanel):
missing_padding = len(favicon_b64) % 4
if missing_padding:
favicon_b64 += '=' * (4 - missing_padding)
try:
favicon_data = base64.b64decode(favicon_b64.split(',')[1])
image = Image.open(BytesIO(favicon_data))
resized_image = image.resize((51, 51), Image.LANCZOS)
favicon_image = ImageTk.PhotoImage(resized_image)
favicon_label = customtkinter.CTkLabel(
master=worldPanel,
image=favicon_image,
width=51,
height=51,
text=""
)
favicon_label.place(x=35, rely=0.5, anchor=tkinter.CENTER)
except Exception as e:
print(f"Error loading favicon: {e}")
def spawnThePanelOfTheWorld(java_tk, ms, UserName, PassIsEnabled, noOneHostsWorldLabel):
global count
worldPanel = customtkinter.CTkFrame(
master=java_tk,
fg_color=”#141414″,
width=1095,
height=74,
corner_radius=5
)
worldPanel.place(x=298, y=((96 * count) + margin))
noOneHostsWorldLabel.place_forget()
count += 1
playerCountLabel = customtkinter.CTkLabel(
master=worldPanel,
text=f"{ms.current_players} из {ms.max_players}",
text_color="white",
font=("Microsoft YaHei", 18),
fg_color="#141414"
)
playerCountLabel.place(x=729, rely=0.5, anchor=tkinter.CENTER)
nameOfWorldLabel = customtkinter.CTkLabel(
master=worldPanel,
text=f"{ms.stripped_motd}",
text_color="white",
fg_color="#141414",
width=176,
font=("Microsoft YaHei", 14.25)
)
nameOfWorldLabel.place(x=69, y=12)
UserName = UserName[0] if UserName else ""
password_status = " • С паролем" if PassIsEnabled != "False" else ""
pingAndVersion = customtkinter.CTkLabel(
master=worldPanel,
text=f"• {ms.version} • Пинг {ms.latency} ms • Хост {UserName}{password_status}",
text_color="#5A5A5A",
fg_color="#141414"
)
pingAndVersion.place(x=70, y=44)
playButton = customtkinter.CTkButton(
master=worldPanel,
corner_radius=10,
fg_color="#253d3d",
font=("Microsoft YaHei", 9.75),
text="Скопировать адрес",
width=180,
height=45,
command=lambda: copyLink(ms.address, ms.port)
)
playButton.place(x=892, y=12)
favicon = Favicon()
favicon.load_favicon(ms.favicon_b64, worldPanel)
def loadWorlds(java_tk, noOneHostsWorldLabel):
dt = connect() # Assign the returned value to dt
if dt is not None: # Check if dt is not None
print(dt.head()) # Print the first few rows of the DataFrame to check its contents
for index, localWorld in dt.iterrows():
ip = localWorld[“ip”]
port = localWorld[“port”]
id = localWorld[“id”]
userID = localWorld[“userID”],
UserName = localWorld[“UserName”],
PassIsEnabled = localWorld[“PassIsEnabled”]
checkWorld(java_tk, ip, port, id, UserName, PassIsEnabled, noOneHostsWorldLabel)
else:
print(“Failed to fetch data from the database.”)
def checkWorld(java_tk, ip, port, id, UserName, PassIsEnabled, noOneHostsWorldLabel):
ms = minestat.MineStat(str(ip), int(port))
if ms.online:
spawnThePanelOfTheWorld(java_tk, ms, UserName, PassIsEnabled, noOneHostsWorldLabel)
`
I’ve tried reading the documentation and searching online, but unfortunately, I haven’t found anything understandable for myself (perhaps nobody has encountered this issue, or I don’t know how to search properly 🙁 ) I’ve already said what I’m expecting: “I’m trying to create a display of local world panels that people can upload to my database. Each of these local worlds has different data, and it’s precisely this data that distinguishes them. Each world is displayed on a panel”
Belium is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.