Python code doesn't execute. exiting code 0
Question:
i am on a project about square puzzle but my program is not responding to execution call.its about a simple square puzzle game with highest score feature. i have written this code but i cant run it. exiting with code 0 but did not showing anything on the screen
here is my code:
import tkinter as tk
import random
import os
import time
window = tk.Tk()
window.geometry("500x500")
window.title("Parça Parça")
author_label = tk.Label(window, text="xxx", font=("Arial", 10))
author_label.pack(side="bottom", pady=10)
title_label = tk.Label(window, text="Parça Parça", font=("Arial", 20, "bold"))
title_label.pack(side="top", pady=10)
name_label = tk.Label(window, text="Adınızı girin:")
name_label.pack()
name_entry = tk.Entry(window)
name_entry.pack()
load_button = tk.Button(window, text="Resim Yükle")
load_button.pack()
def select_image():
file_path = tk.filedialog.askopenfilename()
if file_path:
image = image.open(file_path)
image = image.resize((400, 400))
image_list = []
for i in range(0, 400, 100):
for j in range(0, 400, 100):
piece = image.crop((i, j, i+100, j+100))
image_list.append(piece)
random.shuffle(image_list)
grid = [image_list[i:i+4] for i in range(0, len(image_list), 4)]
node_grid = []
for i in range(4):
row = []
for j in range(4):
piece = grid[i][j]
node = Node(piece, i, j)
row.append(node)
node_grid.append(row)
game_start(node_grid)
load_button.config(command=select_image)
class Node:
def __init__(self, piece, row, col):
self.piece = piece
self.row = row
self.col = col
self.button = None
self.next = None
self.prev = None
def create_linked_list(grid):
head = None
for i in range(len(grid)):
for j in range(len(grid)):
node = grid[i][j]
if not head:
head = node
else:
curr = head
while curr.next:
curr = curr.next
curr.next = node
node.prev = curr
return head
def shuffle_pieces():
global puzzle_pieces
random.shuffle(puzzle_pieces)
for row in range(4):
for col in range(4):
button = puzzle_pieces[row * 4 + col][0]
button.grid(row=row+1, column=col)
def move_piece(button):
global moves, score
empty_row, empty_col = find_empty_position()
button_row, button_col = get_position(button)
if empty_row == button_row and abs(empty_col - button_col) == 1 or
empty_col == button_col and abs(empty_row - button_row) == 1:
swap_buttons(button, empty_row, empty_col, button_row, button_col)
moves += 1
score += 5
score_label.config(text="Score: " + str(score))
check_solution()
def check_pieces():
global correct_pieces
correct_pieces = 0
for piece in pieces:
if piece.current_row == piece.final_row and piece.current_col == piece.final_col:
correct_pieces += 1
if correct_pieces == num_pieces:
messagebox.showinfo("Congratulations", "You have completed the puzzle!")
score = 0
def update_score(correct_move):
global score
if correct_move:
score += 5
else:
score -= 10
score_label.config(text=f"Score: {score}")
# with open('enyuksekskor.txt', 'a') as f:
# f.write(f"{kullanici_adi}: Hamle sayisi: {hamle_sayisi}, Puan: {puan}n")
# def en_yuksek_skorlari_goster():
# skorlar = []
# with open('enyuksekskor.txt', 'r') as f:
# for line in f:
# skorlar.append(line.strip())
# # skorları puanlarına göre sırala
# skorlar.sort(key=lambda x: int(x.split(':')[-1].strip().split()[-1]), reverse=True)
# # en yüksek 10 skoru göster
# skorlar = skorlar[:10]
# skor_text = "n".join(skorlar)
# skor_label.config(text=skor_text)
and when i run the program terminal output is PS C:Usersxxx> & C:/Python311/python.exe c:/Users/xxx/Desktop/xxx.py
Answers:
You need to call the mainloop()
on your instance of Tk()
to start the GUI event loop (and launch the GUI). Add this to the bottom of your script:
if __name__ == '__main__':
window.mainloop()
i am on a project about square puzzle but my program is not responding to execution call.its about a simple square puzzle game with highest score feature. i have written this code but i cant run it. exiting with code 0 but did not showing anything on the screen
here is my code:
import tkinter as tk
import random
import os
import time
window = tk.Tk()
window.geometry("500x500")
window.title("Parça Parça")
author_label = tk.Label(window, text="xxx", font=("Arial", 10))
author_label.pack(side="bottom", pady=10)
title_label = tk.Label(window, text="Parça Parça", font=("Arial", 20, "bold"))
title_label.pack(side="top", pady=10)
name_label = tk.Label(window, text="Adınızı girin:")
name_label.pack()
name_entry = tk.Entry(window)
name_entry.pack()
load_button = tk.Button(window, text="Resim Yükle")
load_button.pack()
def select_image():
file_path = tk.filedialog.askopenfilename()
if file_path:
image = image.open(file_path)
image = image.resize((400, 400))
image_list = []
for i in range(0, 400, 100):
for j in range(0, 400, 100):
piece = image.crop((i, j, i+100, j+100))
image_list.append(piece)
random.shuffle(image_list)
grid = [image_list[i:i+4] for i in range(0, len(image_list), 4)]
node_grid = []
for i in range(4):
row = []
for j in range(4):
piece = grid[i][j]
node = Node(piece, i, j)
row.append(node)
node_grid.append(row)
game_start(node_grid)
load_button.config(command=select_image)
class Node:
def __init__(self, piece, row, col):
self.piece = piece
self.row = row
self.col = col
self.button = None
self.next = None
self.prev = None
def create_linked_list(grid):
head = None
for i in range(len(grid)):
for j in range(len(grid)):
node = grid[i][j]
if not head:
head = node
else:
curr = head
while curr.next:
curr = curr.next
curr.next = node
node.prev = curr
return head
def shuffle_pieces():
global puzzle_pieces
random.shuffle(puzzle_pieces)
for row in range(4):
for col in range(4):
button = puzzle_pieces[row * 4 + col][0]
button.grid(row=row+1, column=col)
def move_piece(button):
global moves, score
empty_row, empty_col = find_empty_position()
button_row, button_col = get_position(button)
if empty_row == button_row and abs(empty_col - button_col) == 1 or
empty_col == button_col and abs(empty_row - button_row) == 1:
swap_buttons(button, empty_row, empty_col, button_row, button_col)
moves += 1
score += 5
score_label.config(text="Score: " + str(score))
check_solution()
def check_pieces():
global correct_pieces
correct_pieces = 0
for piece in pieces:
if piece.current_row == piece.final_row and piece.current_col == piece.final_col:
correct_pieces += 1
if correct_pieces == num_pieces:
messagebox.showinfo("Congratulations", "You have completed the puzzle!")
score = 0
def update_score(correct_move):
global score
if correct_move:
score += 5
else:
score -= 10
score_label.config(text=f"Score: {score}")
# with open('enyuksekskor.txt', 'a') as f:
# f.write(f"{kullanici_adi}: Hamle sayisi: {hamle_sayisi}, Puan: {puan}n")
# def en_yuksek_skorlari_goster():
# skorlar = []
# with open('enyuksekskor.txt', 'r') as f:
# for line in f:
# skorlar.append(line.strip())
# # skorları puanlarına göre sırala
# skorlar.sort(key=lambda x: int(x.split(':')[-1].strip().split()[-1]), reverse=True)
# # en yüksek 10 skoru göster
# skorlar = skorlar[:10]
# skor_text = "n".join(skorlar)
# skor_label.config(text=skor_text)
and when i run the program terminal output is PS C:Usersxxx> & C:/Python311/python.exe c:/Users/xxx/Desktop/xxx.py
You need to call the mainloop()
on your instance of Tk()
to start the GUI event loop (and launch the GUI). Add this to the bottom of your script:
if __name__ == '__main__':
window.mainloop()