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

如何解决初学者锻炼计划-如何使用tkinker将项目添加到特定列表

代码

# 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,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,relheight=0.06)

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

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

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

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

AddEx = tk.Button(root,text="Add",command=click)
AddEx.place(relx=0.25,rely=0.8,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()

解决方法

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

代码:

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,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,relheight=0.06)

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

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

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

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

AddEx = tk.Button(root,text="Add",command=click)
AddEx.place(relx=0.25,rely=0.8,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菜单->选择{{1 }}选项。

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','Reverse Curls','XXX'] 项已添加到与手臂相关的列表中。当然,如果您单击XXX按钮,则随机生成的列表也可以包含此新元素,如下所示:

Arms

GUI:

GUI

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?