Wie Scrollbar mit tkinter
Verfasst: Mittwoch 27. Dezember 2023, 07:12
Hallo zusammen!
ich lerne gerade etwas Python, um genauer zu sein bin ich absoluter Anfänger. Habe jetzt mein erstes Programm mit tkinter-GUI gebastelt.
Wie kann ich eine Scrollbar einbauen? Es soll auch nur soweit Scrollen können, wie Inhalt auf der Seite ist (also nicht drüber hinaus).
Im Allgemeinen generiert hier nur ein Gitter aus Bildern, das bei Klick den Zustand ändert und diesen in einer .txt-File abspeichert.
Danke schonmal!
ich lerne gerade etwas Python, um genauer zu sein bin ich absoluter Anfänger. Habe jetzt mein erstes Programm mit tkinter-GUI gebastelt.
Wie kann ich eine Scrollbar einbauen? Es soll auch nur soweit Scrollen können, wie Inhalt auf der Seite ist (also nicht drüber hinaus).
Im Allgemeinen generiert hier nur ein Gitter aus Bildern, das bei Klick den Zustand ändert und diesen in einer .txt-File abspeichert.
Danke schonmal!
Code: Alles auswählen
import tkinter as tk
from PIL import Image, ImageTk
import os
def on_image_click(row, col):
images[row][col]["grayed"] = not images[row][col]["grayed"]
update_image_state(row, col)
save_state()
def save_state():
with open("state.txt", "w") as file:
for row in images:
for cell in row:
file.write(str(int(cell["grayed"])) + " ")
def update_image_state(row, col):
if images[row][col]["grayed"]:
images[row][col]["label"].config(image=gray_image)
else:
images[row][col]["label"].config(image=photo_images[row][col])
# Initialize the main window
root = tk.Tk()
root.title("JackOfAllChamps v0.0.1")
root.iconbitmap("l.ico")
root.geometry("625x300")
root.configure(bg='#aaf0d1')
# Create a frame for the grid
grid_frame = tk.Frame(root, bg='#aaf0d1', bd=0, relief="solid", highlightbackground='#aaf0d1', highlightcolor='#aaf0d1')
grid_frame.grid(row=1, column=0, columnspan=8, pady=10)
# Add the image above the grid
headline_image = ImageTk.PhotoImage(Image.open(os.path.join("src", "headlineJoac.png")))
headline_label = tk.Label(grid_frame, image=headline_image, bg='#aaf0d1')
headline_label.grid(row=0, column=0, columnspan=8, pady=10)
# Load images and create labels
images = []
gray_image = ImageTk.PhotoImage(Image.open(os.path.join("src", "gray_image.jpeg")))
photo_images = [[None] * 8 for _ in range(3)]
# Load the state
try:
with open("state.txt", "r") as file:
state = [int(bit) for bit in file.read().split()]
except FileNotFoundError:
state = [0] * (3 * 8)
bit_index = 0
for row in range(3):
row_images = []
for col in range(8):
if bit_index < len(state):
cell = {"grayed": bool(state[bit_index]), "row": row, "col": col}
photo_images[row][col] = ImageTk.PhotoImage(Image.open(os.path.join("src", f"image_{row}_{col}.jpeg")))
label = tk.Label(grid_frame, image=photo_images[row][col], bg='#aaf0d1')
label.bind("<Button-1>", lambda event, r=row, c=col: on_image_click(r, c))
label.grid(row=row + 1, column=col, padx=5, pady=5)
cell["label"] = label
row_images.append(cell)
bit_index += 1
images.append(row_images)
# Update the state for correct initial display
for row in range(3):
for col in range(8):
update_image_state(row, col)
# Start the GUI loop
root.mainloop()