81讲,Button.bind("<Button-3>",callback1)报错(58行)
本帖最后由 猪猪虾 于 2020-4-17 09:44 编辑#----------将程序运行期间产生的所有事件运行的结果,记录并保存到一个文件中---------
#编写思路:一个frame里面创建3个按钮,一个按钮实现:单击鼠标右键,显示可选对话框的功能
#另一个按钮:单击鼠标左键显示对话框的功能,点击相应的按钮,创建一个新的界面,只实现相应的功能
#按钮3:保存操作按钮
#----------------------------------------------------------------------------------------------------------
from tkinter import *
#实现单击鼠标右键,显示对话框的功能
root=Tk()
menubar=Menu(root)
frame = Frame(root,width=200,height=200).grid(row=0, column=1, padx=10, pady=5)
#实现单击鼠标左键,显示相应的位置坐标的功能
code_1=[]
code_2=[] #存放操作的结果
def callback1(event):
def callback(event):
print("the location of the mouth:",event.x,event.y)
code_1.append((event.x,event.y))
frame.bind("<Button-1>",callback)
frame.pack()
def callback2(event):
def callback4(event):
code_2.append("return")
code_2.append(' \r\n') #换行
print("return")
def callback5(event):
code_1.append("do again")
code_1.append(' \r\n')
print("do again")
menubar=Menu(root)
menubar.add_command(label="return",command=callback4)
menubar.add_command(label="do again",command=callback5)
def popup(event):
menubar.post(event.x_root,event.y_root)
frame.bind("<Button-3>",popup) #Button-3鼠标右键触发,1是左键,2是中间健
def callback3(event):
Label(frame, text="请输入文件名:").grid(row=1, column=0, sticky= W, padx=10, pady=5)
e = Entry(root).grid(row=2, column=0, sticky= W, padx=10, pady=5)
def show():
title=e.get()
return(title)
#输入结束,用户单击确定按钮,结束输入
Button(frame, text="确定", width=10, command=show).grid(row=3, column=0, sticky= W, padx=10, pady=5)
#保存所执行的事件
title=show()
final_tltle_foddle=title+'.txt'
final_tltle=open(final_tltle_foddle,'w+')
final_tltle.writelines(code_1)
final_tltle.writelines(code_2)
final_tltle.close()
b1=Button(frame, text="单击鼠标右键显示相应的位置坐标的功能", width=30).grid(row=1, column=0, sticky= W, padx=10, pady=5)
b1.bind("<Button-1>",callback1)
b2=Button(frame, text="单击鼠标左键显示对话框的功能", width=30).grid(row=2, column=0, sticky=W, padx=10, pady=5)
b2.bind("<Button-1>",callback2)
b3=Button(frame, text="是否保存当前操作", width=30).grid(row=3, column=0, sticky=W, padx=10, pady=5)
b3.bind("<Button-1>",callback3)
mainloop()
先将 Button() 的实例赋值给变量再 bind(),而且 callback3 没有定义
# ----------将程序运行期间产生的所有事件运行的结果,记录并保存到一个文件中---------
# 编写思路:一个frame里面创建3个按钮,一个按钮实现:单击鼠标右键,显示可选对话框的功能
# 另一个按钮:单击鼠标左键显示对话框的功能,点击相应的按钮,创建一个新的界面,只实现相应的功能
# 按钮3:保存操作按钮
# ----------------------------------------------------------------------------------------------------------
from tkinter import *
# 实现单击鼠标右键,显示对话框的功能
root = Tk()
menubar = Menu(root)
frame = Frame(root, width=200, height=200).grid(row=0, column=1, padx=10, pady=5)
# 实现单击鼠标左键,显示相应的位置坐标的功能
code_1 = []
code_2 = []# 存放操作的结果
def callback1(event):
def callback(event):
print("the location of the mouth:", event.x, event.y)
code_1.append((event.x, event.y))
frame.bind("<Button-1>", callback)
frame.pack()
def callback2(event):
def callback4(event):
code_2.append("return")
code_2.append(' \r\n')# 换行
print("return")
def callback5(event):
code_1.append("do again")
code_1.append(' \r\n')
print("do again")
menubar = Menu(root)
menubar.add_command(label="return", command=callback4)
menubar.add_command(label="do again", command=callback5)
def popup(event):
menubar.post(event.x_root, event.y_root)
frame.bind("<Button-3>", popup)# Button-3鼠标右键触发,1是左键,2是中间健
def callback2(event):
Label(frame, text="请输入文件名:").pack()
e = Entry(root)
e.pack(padx=20, pady=20)
def show():
title = e.get()
return (title)
# 输入结束,用户单击确定按钮,结束输入
Button(frame, text="确定", width=10, command=show).pack()
# 保存所执行的事件
title = show()
final_tltle_foddle = title + '.txt'
final_tltle = open(final_tltle_foddle, 'w+')
final_tltle.writelines(code_1)
final_tltle.writelines(code_2)
final_tltle.close()
b1 = Button(frame, text="单击鼠标右键显示相应的位置坐标的功能", width=30).grid(row=1, column=0, sticky=W, padx=10, pady=5)
b1.bind("<Button-3>", callback1)
b2 = Button(frame, text="单击鼠标左键显示对话框的功能", width=30).grid(row=2, column=0, sticky=W, padx=10, pady=5)
b2.bind("<Button-1>", callback2)
b3 = Button(frame, text="是否保存当前操作", width=30).grid(row=3, column=0, sticky=W, padx=10, pady=5)
b3.bind("<Button-1>", callback3)
mainloop() zltzlt 发表于 2020-4-16 18:42
先将 Button() 的实例赋值给变量再 bind(),而且 callback3 没有定义
改了之后,b1.bind("<Button-1>",callback1),显示AttributeError: 'NoneType' object has no attribute 'bind' 需要两步分开
b1 = tk.Button()
b1.grid()
不能直接tk.Button().grid(),这样不会返回任何东西,所以无法调用Button对象的方法 zltzlt 发表于 2020-4-16 18:42
先将 Button() 的实例赋值给变量再 bind(),而且 callback3 没有定义
头像变了 Mike_python小 发表于 2020-4-17 12:54
头像变了
嗯 zltzlt 发表于 2020-4-17 12:54
嗯
回复的这么快吗{:10_297:}
页:
[1]