初学者训练计划如何使用tkinker将项目添加到特定列表

2024-05-19 01:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

# imports

import random
import tkinter as tk
from tkinter import *
from tkinter import filedialog, Text
import os

# variables for frame size 

H = 600
W = 550

root = tk.Tk()

# canvases

canvas = tk.Canvas(root, height=H, width=W)
canvas.pack()

# grey frame frame for buttons

greyframe1 = tk.Frame(root, bg="#36393F")
greyframe1.place(relx=0.2, rely=0.0, relwidth=0.8, relheight=0.7)

# Red frame for where the buttons are

redframe = tk.Frame(root, bg="#8B3A3A")
redframe.place(relx=0.0, rely=0.0, relwidth=0.2, relheight=1.0)

# workout out lists 

workout_out_list_Arms = ("Dips", " OverHead Press ", " EZ- Bar ", "Side Arm Raises", "Triceps Extensions", "Seated Arm Curls" ,"Reverse Curls")


workout_out_list_Legs = (
    "Lunges", "Front Squats", "Bulgarian Split Squats ", " Dumbbell Stepup", "Romanian Deadlifts", " Squats ",
    " Calf Raises ", " Hip Thrusts", "Loaded Carries ", " Bodyweight Calf Raises")

workout_out_list_Shoulders = (
    "Barbell Shoulder Press", " Seated Dumbbell Front Raise", " Dumbbell Lateral Raise", "Rear Deltoid Machine ",
    " Reverse Cable Crossover")

workout_out_list_Abs = (
    "Sit Ups", "barbell rollouts ", "Barbell Russian Twists", "Flutter Kicks", " Leg Pull-ins ", "Leg-Raises",
    "Knee crunches")

workout_out_list_Back = ("Dead Lifts", " closed grip lat pull-downs", " Seated Rows ", "T-bar rows", " Barbell Rows")

FullExercise_list = [workout_out_list_Abs, workout_out_list_Arms, workout_out_list_Back, workout_out_list_Shoulders,
                        workout_out_list_Legs]

# functions for seeing full list of items in drop down

def shoulderslistfunction():
    print(workout_out_list_Shoulders)


def fullbodylistfunction():
    print(FullExercise_list)


def armslistfunction():
    print(workout_out_list_Arms)


def legslistfunction():
    print(workout_out_list_Legs)


def backlistfunction():
    print(workout_out_list_Back)


def corelistfunction():
    print(workout_out_list_Abs)

# layout

menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="Body Group", menu=subMenu)
subMenu.add_command(label=" Shoulders full list", command=shoulderslistfunction())
subMenu.add_command(label=" Arms full list", command=armslistfunction())
subMenu.add_command(label=" Legs full list", command=legslistfunction())
subMenu.add_command(label=" Back full list", command=backlistfunction())
subMenu.add_command(label=" Abs full list", command=corelistfunction())
subMenu.add_command(label=" Full body full list", command=fullbodylistfunction())


# function for entry section

def getbuttonfunction():
    entryboxresults = abFunction().get()

#  buttons for creating adding to list

def buttonFunction():
    print(random.sample(FullExercise_list, 4))

def abFunction():
    print(random.sample(workout_out_list_Abs, 4))


def armFunction():
    print(random.sample(workout_out_list_Arms, 4))


def legsFunction():
    print(random.sample(workout_out_list_Legs, 4))


def backFunction():
    print(random.sample(workout_out_list_Back, 4))


def shoulderFunction():
    print(random.sample(workout_out_list_Back, 4))


# Creating a entry for a list

def click():
    entered_text = textentry.get()

# buttons for workout

Generate_Workout = tk.Button(redframe, text="Generate Workout ", command=buttonFunction)
Generate_Workout.place(relx=0.0, rely=0.9, relwidth=1.0, relheight=0.06)

Arms = tk.Button(redframe, text="Arms ", command=armFunction)
Arms.place(relx=0.1, rely=0.2, relwidth=0.8, relheight=0.06)

Back = tk.Button(redframe, text="Back", command=backFunction)
Back.place(relx=0.1, rely=0.06, relwidth=0.8, relheight=0.06)

Core = tk.Button(redframe, text="Core ", command=abFunction)
Core.place(relx=0.1, rely=0.35, relwidth=0.8, relheight=0.06)

Shoulders = tk.Button(redframe, text="Shoulders", command=shoulderFunction)
Shoulders.place(relx=0.1, rely=0.45, relwidth=0.8, relheight=0.06)

Legs = tk.Button(redframe, text="Legs ", command=legsFunction)
Legs.place(relx=0.1, rely=0.75, relwidth=0.8, relheight=0.06)

AddEx = tk.Button(root, text="Add", command=click)
AddEx.place(relx=0.25, rely=0.8, relwidth=0.2, relheight=0.06)

def mylabel():
    myLabel = Label(root, )

# entry for adding to list

entry1 = tk.Entry()
canvas.create_window(200, 459, window=entry1)

# label for entry area

label = tk.Label(root, text="Select category first then enter new exercise ")
label.place(relx=0.2, rely=0.7)

label = tk.Label(greyframe1, text=" Workout Generated  ")
label.place(relx=0.3, rely=0.1)

root.mainloop()

Tags: textfordefplacerootoutlabelcommand
1条回答
网友
1楼 · 发布于 2024-05-19 01:17:28

首先,在你的下一个问题中,你应该详细说明你的问题。另一方面,我已经根据您的代码编写了一个工作版本。我更改了代码中的许多地方,因此我决定在代码中写注释作为解释,而不是写一个很长的答案

代码:

import random
import tkinter as tk
from tkinter import *

# Text for the selected category.
SELECTED_CAT = "Selected category: {}"

# variables for frame size
H = 600
W = 550

root = tk.Tk()

# This is a string variable for the Entry widget.
# Itt will contain the written exec.
new_exec_var = tk.StringVar()

# canvases

canvas = tk.Canvas(root, height=H, width=W)
canvas.pack()

# grey frame frame for buttons

greyframe1 = tk.Frame(root, bg="#36393F")
greyframe1.place(relx=0.2, rely=0.0, relwidth=0.8, relheight=0.7)

# Red frame for where the buttons are

redframe = tk.Frame(root, bg="#8B3A3A")
redframe.place(relx=0.0, rely=0.0, relwidth=0.2, relheight=1.0)

# workout out lists
# The following items were not lists they were tuples. They have been corrected. () -> []
workout_out_list_Arms = [
    "Dips",
    " OverHead Press ",
    " EZ- Bar ",
    "Side Arm Raises",
    "Triceps Extensions",
    "Seated Arm Curls",
    "Reverse Curls",
]


workout_out_list_Legs = [
    "Lunges",
    "Front Squats",
    "Bulgarian Split Squats ",
    " Dumbbell Stepup",
    "Romanian Deadlifts",
    " Squats ",
    " Calf Raises ",
    " Hip Thrusts",
    "Loaded Carries ",
    " Bodyweight Calf Raises",
]

workout_out_list_Shoulders = [
    "Barbell Shoulder Press",
    " Seated Dumbbell Front Raise",
    " Dumbbell Lateral Raise",
    "Rear Deltoid Machine ",
    " Reverse Cable Crossover",
]

workout_out_list_Abs = [
    "Sit Ups",
    "barbell rollouts ",
    "Barbell Russian Twists",
    "Flutter Kicks",
    " Leg Pull-ins ",
    "Leg-Raises",
    "Knee crunches",
]

workout_out_list_Back = [
    "Dead Lifts",
    " closed grip lat pull-downs",
    " Seated Rows ",
    "T-bar rows",
    " Barbell Rows",
]

FullExercise_list = [
    workout_out_list_Abs,
    workout_out_list_Arms,
    workout_out_list_Back,
    workout_out_list_Shoulders,
    workout_out_list_Legs,
]

# functions for seeing full list of items in drop down


def shoulderslistfunction():
    print(workout_out_list_Shoulders)


def fullbodylistfunction():
    print(FullExercise_list)


def armslistfunction():
    print(workout_out_list_Arms)


def legslistfunction():
    print(workout_out_list_Legs)


def backlistfunction():
    print(workout_out_list_Back)


def corelistfunction():
    print(workout_out_list_Abs)


# layout

menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="Body Group", menu=subMenu)
# The parentheses are not needed for the command parameter. I have removed them.
subMenu.add_command(label=" Shoulders full list", command=shoulderslistfunction)
subMenu.add_command(label=" Arms full list", command=armslistfunction)
subMenu.add_command(label=" Legs full list", command=legslistfunction)
subMenu.add_command(label=" Back full list", command=backlistfunction)
subMenu.add_command(label=" Abs full list", command=corelistfunction)
subMenu.add_command(label=" Full body full list", command=fullbodylistfunction)


# function for entry section


def getbuttonfunction():
    entryboxresults = abFunction().get()


#  buttons for creating adding to list
# If select a button then the Selected text will contain the currently selected exec type.
def buttonFunction():
    selected_label.configure(text=SELECTED_CAT.format("ALL"))
    print(random.sample(FullExercise_list, 4))


def abFunction():
    selected_label.configure(text=SELECTED_CAT.format("AB"))
    print(random.sample(workout_out_list_Abs, 4))


def armFunction():
    selected_label.configure(text=SELECTED_CAT.format("ARM"))
    print(random.sample(workout_out_list_Arms, 4))


def legsFunction():
    selected_label.configure(text=SELECTED_CAT.format("LEGS"))
    print(random.sample(workout_out_list_Legs, 4))


def backFunction():
    selected_label.configure(text=SELECTED_CAT.format("BACK"))
    print(random.sample(workout_out_list_Back, 4))


def shoulderFunction():
    selected_label.configure(text=SELECTED_CAT.format("SHOULDER"))
    print(random.sample(workout_out_list_Shoulders, 4))


# Creating a entry for a list
# Append the written exec for the related list.
def click():
    entered_text = new_exec_var.get()  # Get the written text
    exec_type = selected_label.cget("text").split()[-1]  # Get the selected category

    # Append the new exec based ont he selected category
    if exec_type == "ALL":
        FullExercise_list.append(entered_text)
    elif exec_type == "AB":
        workout_out_list_Abs.append(entered_text)
    elif exec_type == "ARM":
        workout_out_list_Arms.append(entered_text)
    elif exec_type == "LEGS":
        workout_out_list_Legs.append(entered_text)
    elif exec_type == "BACK":
        workout_out_list_Back.append(entered_text)
    elif exec_type == "SHOULDER":
        workout_out_list_Shoulders.append(entered_text)
    else:
        print("Invalid exec type")
    print("'{}' has been added to '{}'".format(entered_text, exec_type))


# buttons for workout

Generate_Workout = tk.Button(redframe, text="Generate Workout ", command=buttonFunction)
Generate_Workout.place(relx=0.0, rely=0.9, relwidth=1.0, relheight=0.06)

Arms = tk.Button(redframe, text="Arms ", command=armFunction)
Arms.place(relx=0.1, rely=0.2, relwidth=0.8, relheight=0.06)

Back = tk.Button(redframe, text="Back", command=backFunction)
Back.place(relx=0.1, rely=0.06, relwidth=0.8, relheight=0.06)

Core = tk.Button(redframe, text="Core ", command=abFunction)
Core.place(relx=0.1, rely=0.35, relwidth=0.8, relheight=0.06)

Shoulders = tk.Button(redframe, text="Shoulders", command=shoulderFunction)
Shoulders.place(relx=0.1, rely=0.45, relwidth=0.8, relheight=0.06)

Legs = tk.Button(redframe, text="Legs ", command=legsFunction)
Legs.place(relx=0.1, rely=0.75, relwidth=0.8, relheight=0.06)

AddEx = tk.Button(root, text="Add", command=click)
AddEx.place(relx=0.25, rely=0.8, relwidth=0.2, relheight=0.06)


def mylabel():
    myLabel = Label(root,)


# entry for adding to list

new_exec = tk.Entry(root, textvariable=new_exec_var)
new_exec.delete(0, tk.END)
canvas.create_window(200, 459, window=new_exec)

# label for entry area

label = tk.Label(root, text="Select category first then enter new exercise ")
label.place(relx=0.2, rely=0.7)

# Show the selected category
selected_label = tk.Label(root, text=SELECTED_CAT.format(None))
selected_label.place(relx=0.2, rely=0.9)


label = tk.Label(greyframe1, text=" Workout Generated  ")
label.place(relx=0.3, rely=0.1)

root.mainloop()

控制台输出:

流程:单击Arms按钮->;在条目中键入XXX->;单击Add按钮->;单击Body Group菜单->;选择Arms full list选项

>>> python3 test.py 
[' OverHead Press ', 'Dips', 'Seated Arm Curls', 'Reverse Curls']
'XXX' has been added to 'ARM'
['Dips', ' OverHead Press ', ' EZ- Bar ', 'Side Arm Raises', 'Triceps Extensions', 'Seated Arm Curls', 'Reverse Curls', 'XXX']

正如您在上面所看到的,已经为arm相关列表添加了新的XXX项。当然,如果单击Arms按钮,随机生成的列表也可以包含此新元素,如下所示:

>>> python3 test.py 
['Reverse Curls', ' OverHead Press ', 'Triceps Extensions', 'Side Arm Raises']
'XXX' has been added to 'ARM'
['Dips', 'Side Arm Raises', 'XXX', ' EZ- Bar ']
['Reverse Curls', ' EZ- Bar ', 'Triceps Extensions', 'Dips']
['Side Arm Raises', ' EZ- Bar ', 'Seated Arm Curls', 'Reverse Curls']
['Dips', 'XXX', 'Triceps Extensions', 'Reverse Curls']

GUI:

GUI

相关问题 更多 >

    热门问题