ich Bastel gerade an einer Punktetabelle fürs Kniffeln.
Ich hatte zunächst kleinteilig angefangen und eine Liste der einzelnen Namen und die Eingabefelder pro Spieler separat
in Frames angelegt.
Nun habe ich das Problem, dass die Entry-Felder und die Label unterschiedliche Höhe haben, und ich eine zeilenweise Ausrichtung verliere.
Gibt es eine Möglichkeit eine feste Höhe in Pixeln für die einzelnen Widgets zu definieren?
height scheint es leider nicht zu sein.
Minimalbeispiel:
Code: Alles auswählen
import tkinter as tk
class Kniffel(tk.Tk):
def __init__(self):
super().__init__()
self.beschreibung = tk.Frame()
self.beschreibung.pack(side = tk.LEFT)
self.eingabefelder = tk.Frame()
self.eingabefelder.pack(side = tk.RIGHT)
self.labels = [tk.Label(self.beschreibung, text = "Platzhalter") for _ in range(15)]
[label.pack() for label in self.labels]
self.entries = [tk.Entry(self.eingabefelder) for _ in range(15)]
[entry.pack() for entry in self.entries]
if __name__ == '__main__':
app = Kniffel()
app.mainloop()
Optisch wollte ich hierhin:
Code: Alles auswählen
import tkinter as tk
def main():
app = App()
app.mainloop()
class App(tk.Tk):
def __init__(self, **kwargs):
super().__init__(**kwargs)
for i in range(15):
beschreibung = tk.Label(self, text = "Beschreibung: ")
eingabe = tk.Entry(self)
beschreibung.grid(row = i, column = 0)
eingabe.grid(row = i, column = 1)
if __name__ == '__main__':
main()
Kompletter Code:
Code: Alles auswählen
import tkinter as tk
def main():
game = Kniffel()
game.mainloop()
class Kniffel(tk.Tk):
def __init__(self, players = None):
players = ["Hans", "Ute", "Paul"]
super().__init__()
self.title("Kniffel")
self.description = Description()
self.description.grid(column = 1, row = 0, sticky = "ne")
self.players = [Player(name) for name in players]
for i, player in enumerate(self.players):
player.grid(column = i + 1 + 1, row = 0) # +1 Steuerung, +1 Beschreibung
# -- Control Buttons --
self.control = tk.Frame()
self.control.grid(column = 0, row = 0, sticky = "nw")
self.get_points = tk.Button(self.control, text = "Ergebnis", command = self.sum_up)
self.get_points.pack()
self.reset = tk.Button(self.control, text = "zurücksetzen", command = self.reset_points)
self.reset.pack()
self.delete = tk.Button(self.control, text = "lösche Spieler", command = self.remove_player)
self.delete.pack()
self.new_player = tk.Entry(self.control, width = len("neuer Spieler"))
self.new_player.pack()
self.new_player.insert(tk.END, 'neuer Spieler')
self.add_p = tk.Button(self.control, text="hinzufügen", command = self.add_player)
self.add_p.pack()
def sum_up(self):
[player.sum_up() for player in self.players]
def reset_points(self):
[player.reset_points() for player in self.players]
def remove_player(self):
if self.players:
self.players[-1].destroy()
del self.players[-1]
def add_player(self):
if self.new_player.get() != "neuer Spieler":
self.players.append(Player(self.new_player.get()))
self.players[-1].grid(column = len(self.players) + 1, row = 0)
self.new_player.delete(0, tk.END)
self.new_player.insert(0, "neuer Spieler")
class Player(tk.Frame):
def __init__(self, name):
super().__init__()
self.name = tk.Label(self, text = name)
self.eyes = [tk.Entry(self, width = max(len(name),5)) for _ in range(6)]
self.bonus = tk.Label(self, text = " - ")
self.sum = tk.Label(self, text = " - ")
self.specials = [tk.Entry(self, width = max(len(name),5)) for _ in range(7)]
self.sum2 = tk.Label(self, text = " - ")
self.total = tk.Label(self, text = " - ")
# -- Layout --
self.name.pack()
[x.pack() for x in self.eyes]
self.bonus.pack()
self.sum.pack()
[x.pack() for x in self.specials]
self.sum2.pack()
self.total.pack()
def sum_up(self):
summe = 0
try:
summe += int(self.eyes[0].get())
except:
pass
for eye in self.eyes:
try:
summe += int(eye.get())
except:
pass
if summe >= 63:
self.bonus.config(text = "25")
summe += 25
else:
self.bonus.config(text = "0")
self.sum.config(text = str(summe))#text(summe)
summe2 = 0
for special in self.specials:
try:
summe2 += int(special.get())
except:
pass
self.sum2.config(text = summe2)
self.total.config(text = summe + summe2)
def reset_points(self):
self.sum.config(text = " - ")
self.bonus.config(text = " - ")
self.sum2.config(text = " - ")
self.total.config(text = " - ")
[x.delete(0, tk.END) for x in self.eyes]
#[x.insert(0, "") for x in self.eyes]
[x.delete(0, tk.END) for x in self.specials]
#[x.insert(0, "") for x in self.specials]
class Description(tk.Frame):
def __init__(self):
super().__init__()
names = ["Legende", "1: ", "2: ", "3: ", "4: ", "5: ", "6:",
"Bonus:", "Summe:","Full House: ", "kleine Straße: ",
"große Straße: ", "dreier Pasch: ", "vierer Pasch: ", "Chance: ",
"Kniffel: ", "Summe", "Total"]
self.names = [tk.Label(self, text = name) for name in names]
# -- Layout --
[x.pack() for x in self.names]
if __name__ == '__main__':
main()