1. Ich wiederhole mich sehr oft, da ich 9 Helden habe, ich weiß dass man das kürzer machen kann, aber wo muss ich mich da einlesen? Kann jemand ein kurzes Beispiel geben, die Methoden sind ja an sich alle kurz.
2. Mir ist aufgefallen, dass man Labels nicht transparent machen kann oder nur sehr umständlich (habe hier ein paar Ansätze gesehen). Ist das immernoch so? Sollte man dann nicht immer Canvas benutzen? Wo ist der unterschied bei Canvas und Label genau? Ich würde gerne meine win/lose Buttons transparent machen -> Lösung Canvas usen??
3. Wie verändere ich die Textfarbe von der Label.info? Ich will, dass die Zähler weiß sind, also diese ganzen 0 auf dem Bild.
4. Wie mache ich diese weißen Ränder bei den 3 Buttons unten weg, es soll flüssig in den Background übergehen? Oder ist das wieder ein Label vs Canvas Problem?
Hier ist mein Code:
Code: Alles auswählen
# -*- coding: utf-8 -*-
# Unnoetige Imports spaeter rausnehmen, weil das Prgramm laenger braucht zum Starten
from __future__ import division
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize, RegexpTokenizer
import collections
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from tkFileDialog import *
from Tkinter import *
import Tkinter
from PIL import Image, ImageTk
from tkMessageBox import *
from time import sleep
import json
#import Image
#import ImageTk
import Tkconstants
import gzip
import math
import os
import glob
"""
Wichtig falls es mal Probleme gibt: Ich habe Pillow und Pil jetzt drauf, weil PIL bei Anaconda nicht geht
Bei Problemen muss PIL manuell entfernt werden.
Zum Positionieren immer Grid benutzen, pack ist crap³
Super Wichtig: Fuer die Ueberlappung der Bilder, muss/kann man die Reihenfolge des Codes beachten (was zuerst kommt etc)
Auch fuer AttributeError! Deswegen kommt/kam der manchmal. Loesung: Natuerlich existiert result_list noch nicht wenn ich
auf den Button klicke. Ich muss vorher laden, damit er erstellt wird, dann geht das.
Drauf achten ausserhalb der init keine neuen Attribute einfuegen
Wenn ich Transparente Bilder haben will, dann Canvas benutzen.
"""
class Hearthstone_Statistiks(object):
def __init__(self):
self.root = Tk()
self.root.title("Hearthstone Statistics by MC The King")
self.root.resizable(0, 0)
self.root.geometry('600x650')
bg_image = Image.open("bg3_650_and_heroes.png")
background_image = ImageTk.PhotoImage(bg_image)
myvar=Tkinter.Label(self.root,image = background_image)
myvar.place(x=0, y=0, relwidth=1, relheight=1)
# self.root.config(background = background_image)
self.save_data = []
"""
# Hier sind die Bilder fuer die Klassen
mage_image = Image.open("border_vorlage_mage.png")
mage_image_use = ImageTk.PhotoImage(mage_image)
my_mage=Tkinter.Label(self.root,image = mage_image_use)
# my_mage.pack(anchor = NW)
my_mage.place(x = 0, y = 0)
druid_image = Image.open("border_vorlage_druid.png")
druid_image_use = ImageTk.PhotoImage(druid_image)
my_druid=Tkinter.Label(self.root,image = druid_image_use)
my_druid.place(x = 200, y = 0)
warrior_image = Image.open("border_vorlage_druid.png")
warrior_image_use = ImageTk.PhotoImage(warrior_image)
my_warrior=Tkinter.Label(self.root,image = warrior_image_use)
my_warrior.place(x = 400, y = 0)
priest_image = Image.open("border_vorlage_druid.png")
priest_image_use = ImageTk.PhotoImage(priest_image)
my_priest=Tkinter.Label(self.root,image = priest_image_use)
my_priest.place(x = 0, y = 200)
warlock_image = Image.open("border_vorlage_druid.png")
warlock_image_use = ImageTk.PhotoImage(warlock_image)
my_warlock=Tkinter.Label(self.root,image = warlock_image_use)
my_warlock.place(x = 200, y = 200)
pala_image = Image.open("border_vorlage_druid.png")
pala_image_use = ImageTk.PhotoImage(pala_image)
my_pala=Tkinter.Label(self.root,image = pala_image_use)
my_pala.place(x = 400, y = 200)
rogue_image = Image.open("border_vorlage_druid.png")
rogue_image_use = ImageTk.PhotoImage(rogue_image)
my_rogue=Tkinter.Label(self.root,image = rogue_image_use)
my_rogue.place(x = 0, y = 400)
hunter_image = Image.open("border_vorlage_druid.png")
hunter_image_use = ImageTk.PhotoImage(hunter_image)
my_hunter=Tkinter.Label(self.root,image = hunter_image_use)
my_hunter.place(x = 200, y = 400)
shaman_image = Image.open("border_vorlage_druid.png")
shaman_image_use = ImageTk.PhotoImage(shaman_image)
my_shaman=Tkinter.Label(self.root,image = shaman_image_use)
my_shaman.place(x = 400, y = 400)
"""
# Macht die Hochzaehlung fuer den win vom Mage und Initialisierung
self.magewin_count = 0
self.magelose_count = 0
self.count = IntVar()
self.count.set(self.magewin_count)
self.info = Label(self.root, textvariable = self.count)
self.info.config(background = "#6074AC")
self.info.place(x = 82, y = 155)
# print dir(self.info)
# Macht die Hochzaehlung fuer den win vom Druid und Initialisierung
self.druidwin_count = 0
self.druidlose_count = 0
self.count_dw = IntVar()
self.count_dw.set(self.druidwin_count)
self.info_dw = Label(self.root, textvariable = self.count_dw)
self.info_dw.config(background = "#6074AC")
self.info_dw.place(x = 282, y = 155)
# Macht die Hochzaehlung fuer den win vom Warrior und Initialisierung
self.warriorwin_count = 0
self.warriorlose_count = 0
self.count_ww = IntVar()
self.count_ww.set(self.warriorwin_count)
self.info_ww = Label(self.root, textvariable = self.count_ww)
self.info_ww.config(background = "#6074AC")
self.info_ww.place(x = 482, y = 155)
# Macht die Hochzaehlung fuer den win vom Priest und Initialisierung
self.priestwin_count = 0
self.priestlose_count = 0
self.count_pw = IntVar()
self.count_pw.set(self.priestwin_count)
self.info_pw = Label(self.root, textvariable = self.count_pw)
self.info_pw.config(background = "#6074AC")
self.info_pw.place(x = 82, y = 355)
# Macht die Hochzaehlung fuer den win vom Warlock und Initialisierung
self.warlockwin_count = 0
self.warlocklose_count = 0
self.count_wlw = IntVar()
self.count_wlw.set(self.warlockwin_count)
self.info_wlw = Label(self.root, textvariable = self.count_wlw)
self.info_wlw.config(background = "#6074AC")
self.info_wlw.place(x = 282, y = 355)
# Macht die Hochzaehlung fuer den win vom Pala und Initialisierung
self.palawin_count = 0
self.palalose_count = 0
self.count_plw = IntVar()
self.count_plw.set(self.palawin_count)
self.info_plw = Label(self.root, textvariable = self.count_plw)
self.info_plw.config(background = "#6074AC")
self.info_plw.place(x = 482, y = 355)
# Macht die Hochzaehlung fuer den win vom Rogue und Initialisierung
self.roguewin_count = 0
self.roguelose_count = 0
self.count_rgw = IntVar()
self.count_rgw.set(self.roguewin_count)
self.info_rgw = Label(self.root, textvariable = self.count_rgw)
self.info_rgw.config(background = "#6074AC")
self.info_rgw.place(x = 82, y = 555)
# Macht die Hochzaehlung fuer den win vom Hunter und Initialisierung
self.hunterwin_count = 0
self.hunterlose_count = 0
self.count_huw = IntVar()
self.count_huw.set(self.hunterwin_count)
self.info_huw = Label(self.root, textvariable = self.count_huw)
self.info_huw.config(background = "#6074AC")
self.info_huw.place(x = 282, y = 555)
# Macht die Hochzaehlung fuer den win vom Shaman und Initialisierung
self.shamanwin_count = 0
self.shamanlose_count = 0
self.count_shw = IntVar()
self.count_shw.set(self.shamanwin_count)
self.info_shw = Label(self.root, textvariable = self.count_shw)
self.info_shw.config(background = "#6074AC")
self.info_shw.place(x = 482, y = 555)
# Macht die Hochzaehlung fuer den lose vom Mage
self.count_ml = IntVar()
self.count_ml.set(self.magelose_count)
self.info_ml = Label(self.root, textvariable = self.count_ml)
self.info_ml.config(background = "#6074AC")
self.info_ml.place(x = 110, y = 155)
# Macht die Hochzaehlung fuer den lose vom Druid
self.count_dl = IntVar()
self.count_dl.set(self.druidlose_count)
self.info_dl = Label(self.root, textvariable = self.count_dl)
self.info_dl.config(background = "#6074AC")
self.info_dl.place(x = 310, y = 155)
# Macht die Hochzaehlung fuer den lose vom Warrior
self.count_wl = IntVar()
self.count_wl.set(self.warriorlose_count)
self.info_wl = Label(self.root, textvariable = self.count_wl)
self.info_wl.config(background = "#6074AC")
self.info_wl.place(x = 510, y = 155)
# Macht die Hochzaehlung fuer den lose vom Priest
self.count_pl = IntVar()
self.count_pl.set(self.priestlose_count)
self.info_pl = Label(self.root, textvariable = self.count_pl)
self.info_pl.config(background = "#6074AC")
self.info_pl.place(x = 110, y = 355)
# Macht die Hochzaehlung fuer den lose vom Warlock
self.count_wll = IntVar()
self.count_wll.set(self.warlocklose_count)
self.info_wll = Label(self.root, textvariable = self.count_wll)
self.info_wll.config(background = "#6074AC")
self.info_wll.place(x = 310, y = 355)
# Macht die Hochzaehlung fuer den lose vom Pala
self.count_pll = IntVar()
self.count_pll.set(self.palalose_count)
self.info_pll = Label(self.root, textvariable = self.count_pll)
self.info_pll.config(background = "#6074AC")
self.info_pll.place(x = 510, y = 355)
# Macht die Hochzaehlung fuer den lose vom Rogue
self.count_rgl = IntVar()
self.count_rgl.set(self.roguelose_count)
self.info_rgl = Label(self.root, textvariable = self.count_rgl)
self.info_rgl.config(background = "#6074AC")
self.info_rgl.place(x = 110, y = 555)
# Macht die Hochzaehlung fuer den lose vom Hunter
self.count_hul = IntVar()
self.count_hul.set(self.hunterlose_count)
self.info_hul = Label(self.root, textvariable = self.count_hul)
self.info_hul.config(background = "#6074AC")
self.info_hul.place(x = 310, y = 555)
# Macht die Hochzaehlung fuer den lose vom Shaman
self.count_shl = IntVar()
self.count_shl.set(self.shamanlose_count)
self.info_shl = Label(self.root, textvariable = self.count_shl)
self.info_shl.config(background = "#6074AC")
self.info_shl.place(x = 510, y = 555)
#erstellt die Buttons und deren Bilder
win_image_open = Image.open("win_counter_image.png")
win_image_use = ImageTk.PhotoImage(win_image_open)
self.magewin = Button(self.root, image = win_image_use, command=self.magewin)
self.magewin.place(x = 0, y = 130)
self.druidwin = Button(self.root, image = win_image_use, command=self.druidwin)
self.druidwin.place(x = 200, y = 130)
self.warriorwin = Button(self.root, image = win_image_use, command=self.warriorwin)
self.warriorwin.place(x = 400, y = 130)
self.priestwin = Button(self.root, image = win_image_use, command=self.priestwin)
self.priestwin.place(x = 0, y = 330)
self.warlockwin = Button(self.root, image = win_image_use, command=self.warlockwin)
self.warlockwin.place(x = 200, y = 330)
self.palawin = Button(self.root, image = win_image_use, command=self.palawin)
self.palawin.place(x = 400, y = 330)
self.roguewin = Button(self.root, image = win_image_use, command=self.roguewin)
self.roguewin.place(x = 0, y = 530)
self.hunterwin = Button(self.root, image = win_image_use, command=self.hunterwin)
self.hunterwin.place(x = 200, y = 530)
self.shamanwin = Button(self.root, image = win_image_use, command=self.shamanwin)
self.shamanwin.place(x = 400, y = 530)
lose_image_open = Image.open("lose_counter_image.png")
lose_image_use = ImageTk.PhotoImage(lose_image_open)
self.magelose = Button(self.root, image = lose_image_use, command=self.magelose)
self.magelose.place(x = 130, y = 130)
self.druidlose = Button(self.root, image = lose_image_use, command=self.druidlose)
self.druidlose.place(x = 330, y = 130)
self.warriorlose = Button(self.root, image = lose_image_use, command=self.warriorlose)
self.warriorlose.place(x = 530, y = 130)
self.priestlose = Button(self.root, image = lose_image_use, command=self.priestlose)
self.priestlose.place(x = 130, y = 330)
self.warlocklose = Button(self.root, image = lose_image_use, command=self.warlocklose)
self.warlocklose.place(x = 330, y = 330)
self.palalose = Button(self.root, image = lose_image_use, command=self.palalose)
self.palalose.place(x = 530, y = 330)
self.roguelose = Button(self.root, image = lose_image_use, command=self.roguelose)
self.roguelose.place(x = 130, y = 530)
self.hunterlose = Button(self.root, image = lose_image_use, command=self.hunterlose)
self.hunterlose.place(x = 330, y = 530)
self.shamanlose = Button(self.root, image = lose_image_use, command=self.shamanlose)
self.shamanlose.place(x = 530, y = 530)
close_image_open = Image.open("exit_button.png")
close_image_use = ImageTk.PhotoImage(close_image_open)
self.close = Button(self.root, image = close_image_use, command=self.callback)
self.close.place(x = 400, y = 600)
save_image_open = Image.open("safe_button.png")
save_image_use = ImageTk.PhotoImage(save_image_open)
self.save = Button(self.root, image = save_image_use, command=self.save)
self.save.place(x = 0, y = 600)
load_image_open = Image.open("load_button.png")
load_image_use = ImageTk.PhotoImage(load_image_open)
self.load = Button(self.root, image = load_image_use, command=self.load)
self.load.place(x = 200, y = 600)
# Diese Zeile fragt, ob das Programm beim Klick auf X geschlossen werden soll
self.root.protocol("WM_DELETE_WINDOW", self.callback)
self.root.mainloop()
# Ab hier beginnen die Methoden
# Fuer das Schließen des Programms
def callback(self):
if askokcancel("Achtung", "Möchten Sie das Programm wirklich beenden?"):
self.root.destroy()
else:
return
def magewin(self):
self.magewin_count = self.magewin_count + 1
# print self.magewin_counter
# sleep(1)
self.count.set(self.magewin_count)
self.root.update_idletasks()
# print self.magewin_counter
def druidwin(self):
self.druidwin_count = self.druidwin_count + 1
self.count_dw.set(self.druidwin_count)
self.root.update_idletasks()
def warriorwin(self):
self.warriorwin_count = self.warriorwin_count + 1
self.count_ww.set(self.warriorwin_count)
self.root.update_idletasks()
def priestwin(self):
self.priestwin_count = self.priestwin_count + 1
self.count_pw.set(self.priestwin_count)
self.root.update_idletasks()
def warlockwin(self):
self.warlockwin_count = self.warlockwin_count + 1
self.count_wlw.set(self.warlockwin_count)
self.root.update_idletasks()
def palawin(self):
self.palawin_count = self.palawin_count + 1
self.count_plw.set(self.palawin_count)
self.root.update_idletasks()
def roguewin(self):
self.roguewin_count = self.roguewin_count + 1
self.count_rgw.set(self.roguewin_count)
self.root.update_idletasks()
def hunterwin(self):
self.hunterwin_count = self.hunterwin_count + 1
self.count_huw.set(self.hunterwin_count)
self.root.update_idletasks()
def shamanwin(self):
self.shamanwin_count = self.shamanwin_count + 1
self.count_shw.set(self.shamanwin_count)
self.root.update_idletasks()
def magelose(self):
self.magelose_count = self.magelose_count + 1
# print self.magelose_counter
# sleep(1)
self.count_ml.set(self.magelose_count)
self.root.update_idletasks()
def druidlose(self):
self.druidlose_count = self.druidlose_count + 1
self.count_dl.set(self.druidlose_count)
self.root.update_idletasks()
def warriorlose(self):
self.warriorlose_count = self.warriorlose_count + 1
self.count_wl.set(self.warriorlose_count)
self.root.update_idletasks()
def priestlose(self):
self.priestlose_count = self.priestlose_count + 1
self.count_pl.set(self.priestlose_count)
self.root.update_idletasks()
def warlocklose(self):
self.warlocklose_count = self.warlocklose_count + 1
self.count_wll.set(self.warlocklose_count)
self.root.update_idletasks()
def palalose(self):
self.palalose_count = self.palalose_count + 1
self.count_pll.set(self.palalose_count)
self.root.update_idletasks()
def roguelose(self):
self.roguelose_count = self.roguelose_count + 1
self.count_rgl.set(self.roguelose_count)
self.root.update_idletasks()
def hunterlose(self):
self.hunterlose_count = self.hunterlose_count + 1
self.count_hul.set(self.hunterlose_count)
self.root.update_idletasks()
def shamanlose(self):
self.shamanlose_count = self.shamanlose_count + 1
self.count_shl.set(self.shamanlose_count)
self.root.update_idletasks()
# Hier werden die Daten als Liste in Stringform in eine .txt Datei gespeichert
def save(self):
self.save_data.append(self.magewin_count)
self.save_data.append(self.magelose_count)
self.save_data.append(self.druidwin_count)
self.save_data.append(self.druidlose_count)
self.save_data.append(self.warriorwin_count)
self.save_data.append(self.warriorlose_count)
self.save_data.append(self.priestwin_count)
self.save_data.append(self.priestlose_count)
self.save_data.append(self.warlockwin_count)
self.save_data.append(self.warlocklose_count)
self.save_data.append(self.palawin_count)
self.save_data.append(self.palalose_count)
self.save_data.append(self.roguewin_count)
self.save_data.append(self.roguelose_count)
self.save_data.append(self.hunterwin_count)
self.save_data.append(self.hunterlose_count)
self.save_data.append(self.shamanwin_count)
self.save_data.append(self.shamanlose_count)
print self.save_data
with open("save_data_file.txt", "w") as datei:
result = str(self.save_data)
datei.write(result)
datei.close()
print "Daten wurden gespeichert"
self.save_data = [] # Damit immer wieder neu gespeichert werden kann und Elemente nicht dran gehangen werden
# Laedt die Datei, das Problem beim Umwandeln in die Liste ist, dass komplett gesplited wird, deswegen Nltk benutzen
def load(self):
try:
with open("save_data_file.txt", "r") as datei:
reader = datei.read()
word_pattern = RegexpTokenizer(r'[0-9]+')
self.result_list = word_pattern.tokenize(reader)
# print result_list, type(result_list)
self.count.set(self.result_list[0])
self.count_ml.set(self.result_list[1])
self.count_dw.set(self.result_list[2])
self.count_dl.set(self.result_list[3])
self.count_ww.set(self.result_list[4])
self.count_wl.set(self.result_list[5])
self.count_pw.set(self.result_list[6])
self.count_pl.set(self.result_list[7])
self.count_wlw.set(self.result_list[8])
self.count_wll.set(self.result_list[9])
self.count_plw.set(self.result_list[10])
self.count_pll.set(self.result_list[11])
self.count_rgw.set(self.result_list[12])
self.count_rgl.set(self.result_list[13])
self.count_huw.set(self.result_list[14])
self.count_hul.set(self.result_list[15])
self.count_shw.set(self.result_list[16])
self.count_shl.set(self.result_list[17])
self.root.update_idletasks()
self.magewin_count = int(self.result_list[0])
self.magelose_count = int(self.result_list[1])
self.druidwin_count = int(self.result_list[2])
self.druidlose_count = int(self.result_list[3])
self.warriorwin_count = int(self.result_list[4])
self.warriorlose_count = int(self.result_list[5])
self.priestwin_count = int(self.result_list[6])
self.priestlose_count = int(self.result_list[7])
self.warlockwin_count = int(self.result_list[8])
self.warlocklose_count = int(self.result_list[9])
self.palawin_count = int(self.result_list[10])
self.palalose_count = int(self.result_list[11])
self.roguewin_count = int(self.result_list[12])
self.roguelose_count = int(self.result_list[13])
self.hunterwin_count = int(self.result_list[14])
self.hunterlose_count = int(self.result_list[15])
self.shamanwin_count = int(self.result_list[16])
self.shamanlose_count = int(self.result_list[17])
except Exception:
showinfo("Info","Keine Daten zum Laden vorhanden, Speichern Sie erst die Daten")
if __name__ == "__main__":
neues_tool = Hearthstone_Statistiks()
und ein Screen:
Danke Leute!!!