I am making a app to help track scores for a card bidding game. (ie I plan to win x rounds or more then play your cards, one after the other highest card wins the round. count the rounds you won. at the end and assign score) there are many players that could participated, but at any given time there are only 4 players that can play a game.
so far I have been able to create 4 dropdown menu’s to select the 4 players that are currently playing. been able to create 4 labels with the starting score (0) and have created a dropdown menu with the names of the bidding games.
now I want to create a dropdown list consisting of the names that are playing. to do this, I added the function playerupdate to be called when a selection is made in one of the four player selection dropdown menu. and this function should then update the the list of names that are playing. but I don’t know how to do this.
from tkinter import *
from tkinter.ttk import *
root = Tk()
frame = Frame(root)
frame.pack()
#set scores function
def showscore1():
scorelabel1.config(text = score1.get())
def showscore2():
scorelabel2.config(text = score1.get())
def showscore3():
scorelabel3.config(text = score1.get())
def showscore4():
scorelabel4.config(text = score1.get())
# Dropdown menu players
players = [
"",
"Lot",
"Axel",
"Ronnie",
"Tom",
"Luc",
"Bart",
"Frans",
"Pieter",
"We"
]
#update playermenu
clicked1 = StringVar()
clicked2 = StringVar()
clicked3 = StringVar()
clicked4 = StringVar()
clicked1.set( "" )
clicked2.set( "" )
clicked3.set( "" )
clicked4.set( "" )
#dropdown menu games:
games = [
"Rikken",
"Pieken",
"Misere",
"9 alleen",
"open piek",
"open misere",
"10 alleen",
"11 alleen",
"12 alleen",
"13 alleen",
"schoppen Miën"
]
selectgame = StringVar()
selectgame.set( "" )
#keeping the score
score1 = IntVar()
score2 = IntVar()
score3 = IntVar()
score4 = IntVar()
score1.set(0)
score2.set(0)
score3.set(0)
score4.set(0)
def playerupdate(player):
print(player)
#select players
player1 = OptionMenu( frame , clicked1 , *players, command = playerupdate )
player1.grid(row = 2, column = 1)
player2 = OptionMenu( frame , clicked2 , *players, command = playerupdate )
player2.grid(row = 2, column = 2)
player3 = OptionMenu( frame , clicked3 , *players, command = playerupdate )
player3.grid(row = 2, column = 3)
player4 = OptionMenu( frame , clicked4 , *players, command = playerupdate )
player4.grid(row = 2, column = 4)
#show score
scorelabel1 = Label(frame, textvariable= score1)
scorelabel1.grid(row = 3, column = 1)
scorelabel2 = Label(frame, textvariable= score2)
scorelabel2.grid(row = 3, column = 2)
scorelabel3 = Label(frame, textvariable= score3)
scorelabel3.grid(row = 3, column = 3)
scorelabel4 = Label(frame, textvariable= score4)
scorelabel4.grid(row = 3, column = 4)
selectgameplayer = StringVar()
selectgameplayer.set( "" )
gameplayers =[p1,p2,p3,p4,"we"]
gameplayer = OptionMenu( frame, selectgameplayer, *gameplayers)
gameplayer.grid(row = 4, column = 1)
game= OptionMenu( frame , selectgame , *games)
game.grid(row = 4, column = 4)
# Execute tkinter
root.mainloop()
I believe a working solution can be found in the code below. (copied from solution as this seems to do the trick, but have been unable to convert this. for my project. Im getting error message that my objects don’t have an attribute add-command.
this code is working i didn’t added my many trials to conversion.
import tkinter as tk
import time
import calendar
# function to be executed whenever year or month is changed
def on_month_changed(*args):
# Calculating the number of days in the month (It is done only once using the set value)
days = calendar.monthrange(year_var.get(), month_var.get())[1]
# update day OptionMenu
menu = day['menu']
# clear current menu
menu.delete(0, 'end')
# populate days for selected year and month
for d in range(1, days+1):
menu.add_command(label=d, command=tk._setit(day_var, d))
day_var.set(min(day_var.get(), days))
root = tk.Tk()
root.title("Calendar App")
root.geometry("960x540")
# Defining widgets in root
escape_button = tk.Button(master=root, text="x")
escape_button.grid(row=0, column=0)
title = tk.Label(master=root, text="TEST")
title.grid(row=0, column=1)
frame = tk.LabelFrame(master=root, padx=50, pady=50)
frame.grid(row=1, column=1)
# ------- Year, Month, Day -------
today = time.localtime()
# Label
tk.Label(master=frame, text="Year:").grid(row=0, column=0)
# Defining variable and option menu
year_var = tk.IntVar(value=today.tm_year)
years = range(today.tm_year, today.tm_year+12)
year = tk.OptionMenu(frame, year_var, *years, command=on_month_changed)
year.grid(row=0, column=1)
# label
tk.Label(master=frame, text="Month:").grid(row=0, column=2)
# Defining variable and option menu
month_var = tk.IntVar(value=today.tm_mon)
month = tk.OptionMenu(frame, month_var, *range(1,13), command=on_month_changed)
month.grid(row=0, column=3)
# Label
tk.Label(master=frame, text="Day:").grid(row=0, column=4)
# Defining variable and option menu
day_var = tk.IntVar(value=today.tm_mday)
day = tk.OptionMenu(frame, day_var, None)
day.grid(row=0, column=5)
root.mainloop()