我想要“编辑"按钮打开一个新窗口,然后可以编辑显示屏上的值 [英] I want the "Edit" button to open a new window and then be able to edit the values on the display

查看:26
本文介绍了我想要“编辑"按钮打开一个新窗口,然后可以编辑显示屏上的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要编辑"按钮打开一个新窗口,然后可以编辑显示屏上的值.例如,我希望它打开一个带有Daniel Keelagher"的窗口.然后能够从玩过的游戏、进球、助攻等中获得 +1 或 -1.请详细说明你的答案,因为我是个笨蛋,而且是初学者.

I want the "Edit" button to open a new window and then be able to edit the values on the display. For example I want it to open a window with "Daniel Keelagher" and then be able to either +1 or -1 from Games Played, Goals, Assists etc. Please be detailed with your answer as i am dumb and a beginner coder.

#Edit Function

root = Tk()
#Labels
lblTitle = Label(text="Chelsea FC Player Statistics",font=('bold', 15), fg="blue",).grid(row=0, column=1)
#Player Names Labels
lblPlayerNames = Label(text="Player Names",font=('bold')).grid(columnspan=1,row=1, column=1)
lblDKName = Label(text="Daniel Keelagher").grid(columnspan=1,row=2, column=1)
lblJKName = Label(text="Joseph Keelagher").grid(columnspan=1,row=3, column=1)
lblBMName = Label(text="Benjamin Miller").grid(columnspan=1,row=4, column=1)
lblJTName = Label(text="Jordan Terlato").grid(columnspan=1,row=5, column=1)
#Matches Played Labels
lblMatchesPlayed = Label(text="Matches Played",font=('bold')).grid(columnspan=1,row=1, column=2)
lblDKMatches = Label(text="10").grid(columnspan=1,row=2, column=2)
lblJKMatches = Label(text="9").grid(columnspan=1,row=3, column=2)
lblBMMatches = Label(text="9").grid(columnspan=1,row=4, column=2)
lblJTMatches = Label(text="8").grid(columnspan=1,row=5, column=2)
#Goals Labels
lblGoals = Label(text="Goals",font=('bold')).grid(row=1, column=3)
lblDKGoals = Label(text="4").grid(columnspan=1,row=2, column=3)
lblJKGoals = Label(text="2").grid(columnspan=1,row=3, column=3)
lblBMGoals = Label(text="0").grid(columnspan=1,row=4, column=3)
lblJTGoals = Label(text="1").grid(columnspan=1,row=5, column=3)
#Assists Labels
lblAssists = Label(text="Assists",font=('bold')).grid(columnspan=1,row=1, column=4)
lblDKAssists = Label(text="4").grid(columnspan=1,row=2, column=4)
lblJKAssists = Label(text="2").grid(columnspan=1,row=3, column=4)
lblBMAssists = Label(text="0").grid(columnspan=1,row=4, column=4)
lblJTAssists = Label(text="1").grid(columnspan=1,row=5, column=4)
#YellowCards Labels
lblYellowCards = Label(text="YC",font=('bold')).grid(columnspan=1,row=1, column=5)
lblDKYellowCards = Label(text="0").grid(columnspan=1,row=2, column=5)
lblJKYellowCards = Label(text="1").grid(columnspan=1,row=3, column=5)
lblBMYellowCards = Label(text="1").grid(columnspan=1,row=4, column=5)
lblJTYellowCards = Label(text="3").grid(columnspan=1,row=5, column=5)
#RedCards Labels
lblYellowCards = Label(text="RC",font=('bold')).grid(columnspan=1,row=1, column=6)
lblDKRedCards = Label(text="0").grid(columnspan=1,row=2, column=6)
lblJKRedCards = Label(text="0").grid(columnspan=1,row=3, column=6)
lblBMRedCards = Label(text="0").grid(columnspan=1,row=4, column=6)
lblJTRedCards = Label(text="1").grid(columnspan=1,row=5, column=6)


#Buttons
btnDKEdit = Button(root, text="Edit",).grid(columnspan=1,row=2, column=7)
btnJKEdit = Button(root, text="Edit", ).grid(columnspan=1,row=3, column=7)
btnBMEdit = Button(root, text="Edit", ).grid(columnspan=1,row=4,column=7)
btbJTEdit = Button(root, text="Edit",).grid(columnspan=1,row=5, column=7)```

推荐答案

所以这完全是你的方式(至少是我能找到的最接近的方式,也是最糟糕的方式,但你做你):

So here it is completely Your way (at least the closest I could find and also the worst way to go about this but You do You):

# import what is necessary
from tkinter import Tk, Label, Button, IntVar, Toplevel
import json

# first try doing this code of block and if it raises the FileNotFoundError use the other bloc
try:
    # open 'settings.json' file and load the data (import json first)
    with open('settings.json') as file:
        stored_data = json.load(file)

except FileNotFoundError:
    # create sample data to add to the created file
    stored_data = {'Daniel Keelagher': {'Matches Played': 10, 'Goals': 4, 'Assists': 4, 'YC': 0, 'RC': 0},
                   'Joseph Keelagher': {'Matches Played': 9, 'Goals': 2, 'Assists': 2, 'YC': 1, 'RC': 0},
                   'Benjamin Miller': {'Matches Played': 9, 'Goals': 0, 'Assists': 0, 'YC': 1, 'RC': 0},
                   'Jordan Terlato': {'Matches Played': 8, 'Goals': 1, 'Assists': 1, 'YC': 3, 'RC': 1}}
    # create 'settings.json' file if the file was not found
    with open('settings.json', 'w') as file:
        # dump all the data in 'settings.json'
        json.dump(stored_data, file, indent=2)

# define function change which will be responsible for changing data in stored_data dictionary
def change(player, stat, value, operator):  # set required arguments for the function which will be the: player (key in the dictionary, i.e. 'Daniel Keelagher'); stat (key in the player dictionary i.e. 'Goals', 'Assists'); value (refers to the IntVar associated with the respective Label); operator (determines whether value will be subtracted or added)
    # getting the current value of the associated IntVar
    cur_value = value.get()
    # eval to determine the operation if/elif
    if operator == '-':
        # accessing the stored_data key and dictionary key under the stored_data[key]
        stored_data[player][stat] = cur_value - 1
        # setting the IntVar value
        value.set(cur_value - 1)
    elif operator == '+':
        # same as previous but adding value
        stored_data[player][stat] = cur_value + 1
        value.set(cur_value + 1)


# defining edit_dk which will be responsible for editing Daniel Keelagher so such similar functions should be created for all players i.e. edit_jk, edit_bm
def edit_dk():
    # creating a new window for data editing
    tp = Toplevel(root)

    # packing a label to display name
    Label(tp, text='Daniel Keelagher').grid(row=0, column=0, columnspan=15)

    # packing edit buttons and value label for data editing, command is set to change() and can be followed through using previous comments this explanation is for all the Label-Button groups below defined in this function
    Button(tp, text='-', command=lambda: change('Daniel Keelagher', 'Matches Played', dk_matches_var, '-')).grid(row=1, column=0)
    Label(tp, textvariable=dk_matches_var).grid(row=1, column=1)
    Button(tp, text='+', command=lambda: change('Daniel Keelagher', 'Matches Played', dk_matches_var, '+')).grid(row=1, column=2)

    Button(tp, text='-', command=lambda: change('Daniel Keelagher', 'Goals', dk_goals_var, '-')).grid(row=1, column=3)
    Label(tp, textvariable=dk_goals_var).grid(row=1, column=4)
    Button(tp, text='+', command=lambda: change('Daniel Keelagher', 'Goals', dk_goals_var, '+')).grid(row=1, column=5)

    Button(tp, text='-', command=lambda: change('Daniel Keelagher', 'Assists', dk_assists_var, '-')).grid(row=1, column=6)
    Label(tp, textvariable=dk_assists_var).grid(row=1, column=7)
    Button(tp, text='+', command=lambda: change('Daniel Keelagher', 'Assists', dk_assists_var, '+')).grid(row=1, column=8)

    Button(tp, text='-', command=lambda: change('Daniel Keelagher', 'YC', dk_yc_var, '-')).grid(row=1, column=9)
    Label(tp, textvariable=dk_yc_var).grid(row=1, column=10)
    Button(tp, text='+', command=lambda: change('Daniel Keelagher', 'YC', dk_yc_var, '+')).grid(row=1, column=11)

    Button(tp, text='-', command=lambda: change('Daniel Keelagher', 'RC', dk_rc_var, '-')).grid(row=1, column=12)
    Label(tp, textvariable=dk_rc_var).grid(row=1, column=13)
    Button(tp, text='+', command=lambda: change('Daniel Keelagher', 'RC', dk_rc_var, '+')).grid(row=1, column=14)


# initiating Tk class which also creates a window
root = Tk()

# Labels
Label(text="Chelsea FC Player Statistics", font=('bold', 15), fg="blue",).grid(row=0, column=1)

# Player Names Labels
Label(text="Player Names", font='bold').grid(columnspan=1, row=1, column=1)
Label(text="Daniel Keelagher").grid(columnspan=1, row=2, column=1)

# Matches Played Labels
Label(text="Matches Played", font='bold').grid(columnspan=1, row=1, column=2)
# setting up an IntVar for matches: for other players it could be for example bm_matches_var and is related to matches label (below) this explanation fits other IntVars too.
dk_matches_var = IntVar()
# set the variable to statistics the same for the rest
dk_matches_var.set(stored_data['Daniel Keelagher']['Matches Played'])
Label(textvariable=dk_matches_var).grid(columnspan=1, row=2, column=2)


# Goals Labels
Label(text="Goals", font='bold').grid(row=1, column=3)
dk_goals_var = IntVar()
dk_goals_var.set(stored_data['Daniel Keelagher']['Goals'])
Label(textvariable=dk_goals_var).grid(columnspan=1, row=2, column=3)

# Assists Labels
Label(text="Assists", font='bold').grid(columnspan=1, row=1, column=4)
dk_assists_var = IntVar()
dk_assists_var.set(stored_data['Daniel Keelagher']['Assists'])
Label(textvariable=dk_assists_var).grid(columnspan=1, row=2, column=4)

# YellowCards Labels
Label(text="YC", font='bold').grid(columnspan=1, row=1, column=5)
dk_yc_var = IntVar()
dk_yc_var.set(stored_data['Daniel Keelagher']['YC'])
Label(textvariable=dk_yc_var).grid(columnspan=1, row=2, column=5)

# RedCards Labels
Label(text="RC", font='bold').grid(columnspan=1, row=1, column=6)
dk_rc_var = IntVar()
dk_rc_var.set(stored_data['Daniel Keelagher']['RC'])
Label(textvariable=dk_rc_var).grid(columnspan=1, row=2, column=6)


# Buttons for other it could be something like `command=edit_bm`
Button(root, text="Edit", command=edit_dk).grid(columnspan=1, row=2, column=7)

# main loop of tkinter
root.mainloop()

# save file after closing the window
with open('settings.json', 'w') as file:
    json.dump(stored_data, file, indent=2)

这是一个玩家,您必须为字典中的每个玩家都这样做,在首页添加所有这些标签、所有这些变量、按钮,并为每个玩家定义函数(就像我一样在此示例中显示)您还必须匹配那些字典键(如函数中定义的)(仅通过示例进行操作,您应该没问题,但我真的建议您查看我给出的其他答案,研究它们"; 并使用这些,看看教程(正如已经提到的评论中的某个人)或某事,因为这是最糟糕的方法,尤其是因为有很多更简单的方法可以做到这一点,所以这里是你可以做的它但你不应该,因为这只是客观上不必要的)

This is one player, You will have to do this for each of the players in the dictionary, add all those labels in front page, all those variables, buttons, and define functions for each one of them (similarly as I have shown in this example) You will also have to match those dictionary keys (as defined in functions) (just do by example and You should be fine but I really suggest You take a look at the other answers I have given, "study them" and use those, take a look at tutorials (as someone in the comments already mentioned) or sth, because this is the worst way to do this especially because there are a lot simpler ways to do this, so here is how You CAN do it but You shouldn't because this is just objectively unnecessary)

唯一不需要更改的是 change 函数定义以及如果您有疑问.

The only thing You don't have to change is the change function definition and if You have questions as them.

如果您对此感兴趣,我也已经在第一个答案中介绍了保存到文件.

Also I already covered saving to file in the first answer if You are interested in that.

这篇关于我想要“编辑"按钮打开一个新窗口,然后可以编辑显示屏上的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆