Danke für die Antwort, in auto-py-to-exe hat man doch extra eine GUI um die entsprechenden Scripte, Dateien etc zu laden(?)
Code: Alles auswählen
#!/usr/bin/env python3
from tkinter import *
import tkinter.messagebox as mb
from PIL import ImageTk, Image
from screeninfo import get_monitors
from random import choice
from os import system
# Basic Hangman function
def get_word():
words = []
with open('words.txt', 'r') as wordlist:
for line in wordlist:
words.append(line)
chosen_word = choice(words)
return chosen_word
def main_function(btn: Button):
global image_lbl, li_word, correct_guesses, incorrect_guesses
global step_1, step_2, step_3, step_4, step_5, step_6, step_7, step_8, step_9, step_10, step_11
if btn['text'] in li_word:
btn.config(bg='SpringGreen', fg='Black')
indices = [indx for indx, val in enumerate(li_word) if val == btn['text']]
for index in indices:
exec(f'ltr_{index}["text"] = btn["text"]')
correct_guesses += 1
else:
btn.config(bg='Red', fg='White')
incorrect_guesses += 1
exec(f'image_lbl.config(image=step_{incorrect_guesses})')
btn.config(state='disabled')
if correct_guesses == word_length:
image_lbl.config(image=won_image)
mb.showinfo('Geschafft!', 'Du hast gewonnen!\nDer Mann ist froh sich auf dich verlassen zu haben!\nGlückwunsch!')
root.destroy()
system('python main.py')
if incorrect_guesses == 11:
mb.showerror('Der Mann ist tot!', 'Er hat sich auf dich verlassen und du lässt ihn hängen!')
root.destroy()
system('python main.py')
# Command functions for the buttons at the top-right of the screen
def instructions():
mb.showinfo('',
"""Das Spiel ist einfach:
Du hast 5 Versuche den Mann zu retten, bei 6 falsch geratenen Buchstaben ist der Mann nicht mehr zu retten.
Bei weniger als 6 falschen Buchstaben ist der Mann gerettet und wird dir sehr dankbar sein.""")
def end_game():
surety = mb.askyesno('', 'Bist du sicher dass du das Spiel verlassen willst? \nDer aktuelle Stand wird nicht gespeichert')
if surety:
root.destroy()
def reset():
surety = mb.askyesno('', "Bist du sicher das Spiel neu zu starten?")
if surety:
root.destroy()
system('python main.py')
# Variables
buttons_details = [["a", "A", 25, 800], ["b", "B", 125, 800], ["c", "C", 225, 800], ["d", "D", 325, 800],
["e", "E", 425, 800], ["f", "F", 525, 800], ["g", "G", 625, 800], ['h', "H", 725, 800],
['i', "I", 825, 800], ['j', "J", 925, 800], ['k', "K", 1025, 800], ['l', "L", 1125, 800],
['m', "M", 1225, 800], ['n', "N", 25, 900], ['o', "O", 125, 900], ['p', "P", 225, 900],
['q', "Q", 325, 900], ['r', "R", 425, 900], ['s', "S", 525, 900], ['t', "T", 625, 900],
['u', "U", 725, 900], ['v', "V", 825, 900], ['w', "W", 925, 900], ['x', "X", 1025, 900],
['y', "Y", 1125, 900], ['z', "Z", 1225, 900]]
word = get_word()
word_length = len(word)-1
correct_guesses = 0
incorrect_guesses = 0
li_word = [char.upper() for char in word]
# Getting the computer screen's dimensions
monitor = get_monitors()[0]
width = monitor.width - 100
height = monitor.height - 100
# Initializing the window
root = Tk()
root.title('Hangman')
root.geometry(f'{width}x{height}')
root.resizable(0, 0)
root.config(bg='White')
root.positionfrom('user')
# Title Label
Label(text='Simones Galgenmännchen', font=("Comic Sans MS", 32, 'bold'), bg='White').place(relx=0.35, y=0)
# Asking if the user wants to play
start_ques = mb.askyesno('Geschichte',
'''Der Mann — ja, das ist sein Name — wurde zu unrecht inhaftiert und steht kurz vor der Todesstrafe.
Du kannst ihn jedoch retten! Du kannst 5 Mal falsche Buchstaben raten um das Rätsel des gesuchten Wortes
zu lösen und den Mann zu retten. Schaffst du es nicht wird das Urteil des Mannes vollstreckt.
Möchtest du versuchen das Rätsel zu lösen?
\n''')
if not start_ques:
root.destroy()
exit()
# Creating all the steps images in PhotoImage format
step_0 = ImageTk.PhotoImage(Image.open('Steps Images/Step 0.png'))
step_1 = ImageTk.PhotoImage(Image.open('Steps Images/Step 1.png'))
step_2 = ImageTk.PhotoImage(Image.open('Steps Images/Step 2.png'))
step_3 = ImageTk.PhotoImage(Image.open('Steps Images/Step 3.png'))
step_4 = ImageTk.PhotoImage(Image.open('Steps Images/Step 4.png'))
step_5 = ImageTk.PhotoImage(Image.open('Steps Images/Step 5.png'))
step_6 = ImageTk.PhotoImage(Image.open('Steps Images/Step 6.png'))
step_7 = ImageTk.PhotoImage(Image.open('Steps Images/Step 7.png'))
step_8 = ImageTk.PhotoImage(Image.open('Steps Images/Step 8.png'))
step_9 = ImageTk.PhotoImage(Image.open('Steps Images/Step 9.png'))
step_10 = ImageTk.PhotoImage(Image.open('Steps Images/Step 10.png'))
step_11 = ImageTk.PhotoImage(Image.open('Steps Images/Step 11.png'))
won_image = ImageTk.PhotoImage(Image.open('Steps Images/Winner.png'))
image_lbl = Label(root, image=step_0, bg='White')
image_lbl.place(relx=0.15, rely=0.1)
# Placing the word on the window
Label(root, text='Zu erratenes Wort:', font=("Georgia", 30, 'bold'), bg='White').place(relx=0.635, rely=0.3)
x = 0.6
i = 0
while i < word_length:
exec(f'''ltr_{i} = Label(root, text="_", bg="White", font=("Georgia", 50, 'bold')); ltr_{i}.place(relx={x}, rely=0.45)''')
x += 0.04
i += 1
# Creating all the letter buttons
for button in buttons_details:
exec(f'''{button[0]}_btn = Button(root, text=button[1], width=9, height=3, bg="DeepSkyBlue",
command=lambda: main_function({button[0]}_btn))''')
exec(f'{button[0]}_btn.place(x=button[2], y=button[3])')
Button(root, text=' Anleitung ', font=("PlayfairDisplay", 12, 'bold'), bg='DeepSkyBlue',
command=instructions).place(x=1125, y=100)
Button(root, text='Beenden', font=("PlayfairDisplay", 12, 'bold'), width=13, bg='DeepSkyBlue',
command=end_game).place(x=1125, y=250)
Button(root, text='Neustart', font=("PlayfairDisplay", 12, 'bold'), width=13, bg='DeepSkyBlue',
command=reset).place(x=1125, y=175)
# Finalizing the window
root.mainloop()