Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按钮错误,但我得到索引器:列表索引超出范围_Python_Tkinter_Python 3.6 - Fatal编程技术网

Python 按钮错误,但我得到索引器:列表索引超出范围

Python 按钮错误,但我得到索引器:列表索引超出范围,python,tkinter,python-3.6,Python,Tkinter,Python 3.6,我的界面上有两个按钮。我希望当我单击它们或点击回车键时,它们都能够调用各自的函数 我遇到的问题是,当我按下回车键时,即使前一个按钮具有焦点,也只会激活整个焦点中的最后一个按钮。如何解决此问题。 欢迎并感谢有用的答案 这就是问题所在: from tkinter import * w = Tk() def startProgram(event = None): print('Program Starting') def readyContent(event = None): p

我的界面上有两个按钮。我希望当我单击它们或点击
回车键时,它们都能够调用各自的函数
我遇到的问题是,当我按下
回车键时,即使前一个按钮具有焦点,也只会激活整个焦点中的最后一个按钮。如何解决此问题。
欢迎并感谢有用的答案

这就是问题所在:

from tkinter import *

w = Tk()

def startProgram(event = None):
    print('Program Starting')

def readyContent(event = None):
    print('Content being prepared')

# Buttons
Button(text='Prepare', command=readyContent).grid(row=10,column=2)
w.bind('<Return>',readyContent) # Binds the Return key to a Function
Button(text='Start', command=startProgram).grid(row=10,column=3)
w.bind('<Return>',startProgram) # Binds the Return key to a Function

w.mainloop()
从tkinter导入*
w=Tk()
def startProgram(事件=无):
打印('程序启动')
def readyContent(事件=无):
打印('正在准备的内容')
#钮扣
按钮(text='Prepare',command=readyContent).grid(行=10,列=2)
w、 绑定(“”,readyContent)#将返回键绑定到函数
按钮(text='Start',command=startProgram).grid(行=10,列=3)
w、 bind(“”,startProgram)#将返回键绑定到函数
w、 mainloop()

当您点击准备开始按钮时,您会得到正在准备的内容或分别开始的节目。当您使用
选项卡键为一个按钮或另一个按钮提供焦点时,不会发生类似的情况。即使焦点准备按钮上,当您点击
输入
时,您会得到:程序启动

这就是我问题的解决方案。 我希望它能帮助其他和我有同样问题的人

from tkinter import *

w = Tk()

def startProgram(event = None):
    print('Program Starting')

def readyContent(event = None):
    print('Content being prepared')

# Buttons
btn1 = Button(text='Prepare', command=readyContent)
btn1.grid(row=10,column=2)
btn1.bind('<Return>',readyContent) # Binds the Return key to a Function
btn2 = Button(text='Start', command=startProgram)
btn2.grid(row=10,column=3)
btn2.bind('<Return>',startProgram) # Binds the Return key to a Function

w.mainloop()
从tkinter导入*
w=Tk()
def startProgram(事件=无):
打印('程序启动')
def readyContent(事件=无):
打印('正在准备的内容')
#钮扣
btn1=按钮(text='Prepare',command=readyContent)
btn1.网格(行=10,列=2)
bind(“”,readyContent)#将返回键绑定到函数
btn2=按钮(text='Start',command=startProgram)
btn2.网格(行=10,列=3)
bind(“”,startProgram)#将返回键绑定到函数
w、 mainloop()

祝你过得愉快!:)

哪个按钮有焦点在这里是完全不相关的,因为您没有将任何内容绑定到单个按钮。请包含您错误的完整回溯,以便我们可以确定哪一行是焦点issue@jasonharper-感谢双方的投入。我是新来的。我想我必须将函数绑定到窗口。在另一个项目中,我只有一个按钮,我想这就是为什么这个公式工作顺利的原因。当我不得不处理按钮时,全景发生了变化。谢谢你的评论。我将发布解决方案,以防有人有同样的问题。再次感谢。