【问题标题】:How to input values into different entry boxes using buttons?如何使用按钮将值输入到不同的输入框中?
【发布时间】:2021-06-08 12:58:17
【问题描述】:

我对 python 非常陌生,我正在尝试制作一个可以跟踪篮球比赛中团队成员统计数据的程序。我对设计没有任何问题,但功能是一个难题,我不确定如何拥有它,以便我可以按下按钮并将正确的积分和统计数据分配给正确的团队成员。

我希望能够选择输入框,然后按其中一个按钮来添加积分/统计数据,或者使用菜单功能并让它以这种方式工作。

import sys
from tkinter import *
from tkinter import messagebox
 
newWindow=Tk()
newWindow.title("Stats")
newWindow.geometry("665x300")
newWindow.configure(bg='light blue')
  
Label(newWindow, text ="", bg='light blue').grid(row=9, column=1)
twoButton = Button(newWindow,text = '2pt +').grid(row=10, column=3)
threeButton = Button(newWindow,text = '3pt +').grid(row=10, column=4)
assistButton = Button(newWindow,text = 'Assist +').grid(row=10, column=5)
reboundButton = Button(newWindow,text = 'Rebound +').grid(row=10, column=6)
stealButton = Button(newWindow,text = 'Steal +').grid(row=10, column=7)
blockButton = Button(newWindow,text = 'Block +').grid(row=10, column=8)
turnoverButton = Button(newWindow,text = 'Turnover +').grid(row=10, column=9) 

Label(newWindow, text ="john:", bg='light blue').grid(row=2, column=1)
Label(newWindow, text ="simon:", bg='light blue').grid(row=3, column=1)
Label(newWindow, text ="joel:", bg='light blue').grid(row=5, column=1)
Label(newWindow, text ="kaur:", bg='light blue').grid(row=6, column=1)
Label(newWindow, text ="genom:", bg='light blue').grid(row=7, column=1)
Label(newWindow, text ="kieran:", bg='light blue').grid(row=8, column=1)

Label(newWindow, text ="2pts:", bg='light blue').grid(row=1, column=2)
Label(newWindow, text ="3pts:", bg='light blue').grid(row=1, column=3)
Label(newWindow, text ="Assists:", bg='light blue').grid(row=1, column=4)
Label(newWindow, text ="Rebounds:", bg='light blue').grid(row=1, column=5)
Label(newWindow, text ="Steals:", bg='light blue').grid(row=1, column=6)
Label(newWindow, text ="Blocks:", bg='light blue').grid(row=1, column=7)
Label(newWindow, text ="Turnovers:", bg='light blue').grid(row=1, column=8)

johnTwoP = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=2)    
johnThreeP = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=3)    
johnAssists = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=4)
johnRebounds = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=5)
johnSteals = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=6)
johnBlocks = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=7)
johnTO = Entry(newWindow, width = 2, state = 'readonly').grid(row=2, column=8)

simonTwoP = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=2)
simonThreeP = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=3)
simonAssists = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=4)
simonRebounds = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=5)
simonSteals = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=6)
simonBlocks = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=7)
simonTO = Entry(newWindow, width = 2, state = 'readonly').grid(row=3, column=8)

joelTwoP = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=2)
joelThreeP = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=3)
joelAssists = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=4)
joelRebounds = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=5)
joelSteals = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=6)
joelBlocks = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=7)
joelTO = Entry(newWindow, width = 2, state = 'readonly').grid(row=5, column=8)

kaurTwoP = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=2)
kaurThreeP = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=3)
kaurAssists = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=4)
kaurRebounds = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=5)
kaurSteals = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=6)
kaurBlocks = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=7)
kaurTO = Entry(newWindow, width = 2, state = 'readonly').grid(row=6, column=8)

genomTwoP = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=2)
genomThreeP = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=3)
genomAssists = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=4)
genomRebounds = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=5)
genomSteals = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=6)
genomBlocks = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=7)
genomTO = Entry(newWindow, width = 2, state = 'readonly').grid(row=7, column=8)

kieranTwoP = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=2)
kieranThreeP = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=3)
kieranAssists = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=4)
kieranRebounds = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=5)
kieranSteals = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=6)
kieranBlocks = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=7)
kieranTO = Entry(newWindow, width = 2, state = 'readonly').grid(row=8, column=8)

selectPlayer = StringVar()
selectPlayer.set("Player")
menu = OptionMenu(newWindow, selectPlayer,"john", "simon", "joel", "kaur", "genom", "kieran").grid(row=10, column=1)

newWindow.mainloop()

【问题讨论】:

  • 可以将数字传递给按钮回调
  • 首先,所有入口变量都是None,因为它们是.grid(...)的结果。

标签: python tkinter button


【解决方案1】:

创建一个球员姓名字典,其值为球员的行号。然后,您可以使用.grid_slaves(row, column) 在该位置获取小部件。

这应该是你增加积分的功能

def addPoints(column):

    widget = newWindow.grid_slaves(row=playerDic[selectPlayer.get()], column=column)[0]

    currentScore = widget.get()

    if currentScore == '':
        currentScore = 0

    widget['state'] = 'normal'
    widget.delete(0, 'end')
    widget.insert(0, int(currentScore) + 1)
    widget['state'] = 'readonly'

你的选项菜单:

selectPlayer = StringVar()
selectPlayer.set("john")

playerDic = {"john": 2, "simon": 3, "joel": 5, "kaur": 6, "genom": 7, "kieran": 8} # row values

menu = OptionMenu(newWindow, selectPlayer, *playerDic.keys()).grid(row=10, column=1)

并且您的按钮必须传递列号。像这样的:

twoButton = Button(newWindow,text = '2pt +', command=lambda :addPoints(2)).grid(row=10, column=3)
threeButton = Button(newWindow,text = '3pt +', command=lambda :addPoints(3)).grid(row=10, column=4)
assistButton = Button(newWindow,text = 'Assist +', command=lambda :addPoints(4)).grid(row=10, column=5)
reboundButton = Button(newWindow,text = 'Rebound +', command=lambda :addPoints(5)).grid(row=10, column=6)
stealButton = Button(newWindow,text = 'Steal +', command=lambda :addPoints(6)).grid(row=10, column=7)
blockButton = Button(newWindow,text = 'Block +', command=lambda :addPoints(7)).grid(row=10, column=8)
turnoverButton = Button(newWindow,text = 'Turnover +', command=lambda :addPoints(8)).grid(row=10, column=9) 

注意:如果您更改条目小部件的行或列,请不要忘记更新 command=lambda :addPoints(column) 中的字典和列号

附带说明:您还应该知道分配给<widget>.grid() 的所有变量都是None,因为.grid() 返回None,所以您的所有变量都几乎没有用。

【讨论】:

    【解决方案2】:

    使用

    Button(newWindow, text='2pt +', command=lambda: click(1))

    创建按钮回调函数并将按钮的值传递给函数

    您的按钮应如下所示

    def click(a):
        print(a)
    
    twoButton = Button(newWindow, text='2pt +', command=lambda: click(1)).grid(row=10, column=3)
    threeButton = Button(newWindow, text='3pt +', command=lambda: click(2)).grid(row=10, column=4)
    assistButton = Button(newWindow, text='Assist +', command=lambda: click(3)).grid(row=10, column=5)
    reboundButton = Button(newWindow, text='Rebound +', command=lambda: click(4)).grid(row=10, column=6)
    stealButton = Button(newWindow, text='Steal +', command=lambda: click(5)).grid(row=10, column=7)
    blockButton = Button(newWindow, text='Block +', command=lambda: click(6)).grid(row=10, column=8)
    turnoverButton = Button(newWindow, text='Turnover +', command=lambda: click(7)).grid(row=10, column=9)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-30
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多